[英]How to use select method on nested model associations in Ruby on Rails
I have three models: 我有三种模式:
class Person
has_many :houses
end
class House
belongs_to :person
has_many :appliances
attr_accessible :address
end
class Appliance
belongs_to :house
attr_accessible :price
end
I want to use the 'select' method to fetch only the 'price' values such as the following: 我想使用“选择”方法来仅获取“价格”值,例如:
Person.joins(:houses => :appliances).select('houses.appliances.price')
But the above didn't work. 但是上述方法不起作用。 However, I tested a similar method like the one below that works:
但是,我测试了一种类似的方法,如下所示:
Person.joins(:houses).select('houses.address')
Any help would be much appreciated. 任何帮助将非常感激。
我想这应该工作:
Person.joins(houses: :appliances).select('appliances.price')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.