没有型号id?

发布于 2024-12-17 13:24:09 字数 729 浏览 1 评论 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

一切正常,直到我引入这个最近的事件...

I get the error

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

when defining a sql call in my model user:

  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} )"

Model code

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

Everything works fine until i introduce this recent_events ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-12-24 13:24:09

我假设这是一个 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 )"

I'm assuming this is a User class or something similar, and I'm assuming when you use #{id}, you're really just trying to use the user's id. So I would do this instead:

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 )"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文