是否有正确的写法,或者我的做法有误?我需要做一个嵌套包含。我找到了 This Link但它似乎不起作用。
def show
@showring = Ring.includes(:stones => :upcharges, :variations).find(params[:id])
end
我有 3 个表... 有很多石头的戒指 has_many upcharges 的石头
模型:
class Ring < ActiveRecord::Base
has_many :stones
end
class Stone < ActiveRecord::Base
has_many :upcharges
belongs_to :ring
end
class Upcharge < ActiveRecord::Base
belongs_to :stone
end
最佳答案
def show
@showring = Ring.includes([{:stones => :upcharges}, :variations]).find(params[:id])
end
添加了一些括号:)
获取所有附加费:
@showring.stones.each do |s|
s.upcharges #Do whatever you need with it
end
选项 2:声明一个 has_many :through
class Ring < ActiveRecord::Base
has_many :stones
has_many :upcharges, :through => :stones
end
然后在 View 中:
<%= @showring.upcharges.to_json.html_safe %>
关于ruby-on-rails - rails3 嵌套包括?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729228/