没有模特身份证?

时间:2011-11-23 14:38:55

标签: sql ruby-on-rails model ruby-on-rails-3.1 self-reference

我收到错误

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

一切正常,直到我介绍这个rec​​ent_events ...

1 个答案:

答案 0 :(得分: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 )"