【发布时间】:2015-07-03 12:51:08
【问题描述】:
我有以下关联
Mobile.rb
has_many :mobile_networks, :dependent => :destroy
has_many :networks, :through => :mobile_networks
Network.rb
has_many :mobiles, :through => :mobile_networks
MobileNetwork.rb
belongs_to :mobile
belongs_to :network
我想查询手机的所有 mobile_networks,然后我想检查所有网络是否处于活动状态,就像我在我的助手中编写了这段代码一样,我正在获得移动
def mobile_state(mobile)
mobile.mobile_networks.each do |e|
e.network.is_checked #true
end
end
所以我需要在查询中执行此操作。请指导我如何做到这一点。
【问题讨论】:
-
mobile.networks.map(&:is_checked)试试这个 -
如果 mobile.networks.where(:ischecked =true)end 这样的事情我需要一个条件
-
Mobile.includes(:networks).where(id: params[:id]).where('is_checked = ?', true)或Mobile.includes(:networks).where(id: params[:id], networks: {is_checked: true}) -
不工作它给我错误 ActionView::Template::Error (undefined method `includes' for #<0xddfdbf0>0xddfdbf0>
标签: ruby-on-rails rails-activerecord has-many-through