0

我得到错误

undefined local variable or method `id' for #<Class:0x007fe1dc4e3bb0>

在我的模型用户中定义 sql 调用时:

  RECENT_EVENTS_CONDITION = "(user_id = #{id}) OR user_id IN (SELECT user_b_id AS user_id FROM user_follows WHERE user_follows.user_a_id = #{id} )"

型号代码

has_many :recent_events,
    :class_name  => "Activity",
    :finder_sql  => 'SELECT activities.* FROM activities
                    WHERE ' + RECENT_EVENTS_CONDITION + '
                    ORDER BY activities.created_at DESC',
    :counter_sql => 'SELECT COUNT(*) FROM activities
                    WHERE ' + RECENT_EVENTS_CONDITION

一切正常,直到我介绍了这个 recent_events ...

4

1 回答 1

0

我假设这是一个User类或类似的东西,并且我假设当您使用 时#{id},您实际上只是在尝试使用用户的 ID。所以我会这样做:

RECENT_EVENTS_CONDITION = "(user_id = users.id) OR user_id IN (SELECT user_b_id AS user_id FROM user_follows WHERE user_follows.user_a_id = users.id )"
于 2011-11-23T15:16:23.940 回答