多対多の一方を更にもう1つの多の要素をもたせたいと思っています。
class Product < ActiveRecord::Base has_many :product_materials has_many :materials, :through => :product_materials end class ProductMaterial < ActiveRecord::Base belongs_to :product belongs_to :material end class Material < ActiveRecord::Base has_many :product_materials has_many :products, :through => :product_materials has_many :material_subs has_many :subs, :through => :material_subs end class MaterialSub < ActiveRecord::Base belongs_to :material belongs_to :sub end class Sub < ActiveRecord::Base has_many :material_subs has_many :materials, :through => :material_subs end
以下のコードのように
@materials = @product.materialsのあとに「.subs」でレコードを持ってこれると思ったのですが、うまく行きませんでした。こういった場合はどのようにかけば反映されるのでしょうか?
def show @product = Product.find(params[:id]) @materials = @product.materials.subs respond_to do |format| format.json { render json: [ @product,@materials ]} end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。