【问题标题】:Querying an atribute of a has many through relationship查询 a 的属性有很多直通关系
【发布时间】:2011-11-15 16:54:36
【问题描述】:

我有一个用户表

has_many :meetings, through: registrations

我想对这样的事情进行 ActiveRecord 查询

User.where("meetings > 0")

但显然我的数据库中没有“会议”列。我应该在哪个模型上添加类方法,该方法会是什么样子?感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    看看在您的关联中使用“计数器缓存”:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    【讨论】:

    • api 文档使它看起来只适用于belongs_to 关系
    • 如果Foo has_many Bars,那么foos表得到一个bars_count字段来存储计数的缓存,它在belongs_to上指定的只是语法
    • PS 谷歌搜索“rails counter cache has_many through”收集到:stackoverflow.com/questions/5256897/…
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2017-06-15
    相关资源
    最近更新 更多