line_item
和account
。 line_item
属于account
。account
有一列is_active
。我正在寻找一种方法来编写Rails范围来查找其帐户line_items
is_active = true
像
这样的东西LineItem.should_display
答案 0 :(得分:4)
修改强>
class LineItem < ActiveRecord::Base
scope :should_display, -> { joins(:account).where(accounts: {is_active: true}) }
end
这会产生与在LineItem模型中添加以下类方法相同的结果。
def self.should_display
joins(:account).where(accounts: {is_active: true})
end
我认为您可以在Rails指南中找到有关Active Record Querying的更多信息:http://guides.rubyonrails.org/active_record_querying.html