2
연결에 대해 혼동스러워합니다. 아래 코드를 작성하려했지만 레일즈가 "undefined method`subs '"를 반환했습니다.연결 규칙을 적용하는 방법
def show
@product = Product.find(params[:id])
@materials = @product.materials.subs
respond_to do |format|
format.json { render json: [ @product,@materials ]}
end
end
제품 모델이 하위 모델과 관련되어 있으며 하위 모델 레코드가 필요합니다. 누군가이 문제에 대해 알고 있으면 해결해주세요.
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
'질문 1 분 ago'위한
subs
을 반환 할 수있다 '대답 1 분 전'- 그건 꽤 빨리 망한다! –은 n + 1에 제공하고, 'includes'를 사용합니다. –
완전히 해결되었습니다. 정말 고맙습니다!! – johnny