레일에서 다 대다 관계에 문제가 있습니다. 세 가지 모달이 있습니다.
주문에는 항목 별 항목을 통해 많은 항목이 있으며 그 반대의 경우도 마찬가지입니다.
Item.find (1) .orders 쿼리는 잘 작동하지만 Order.find (1) .items 시도하면 다음을 반환합니다.
NoMethodError: undefined method `items' for #<Order:0x007fcad3bb3258>
내 코드는 다음과 같습니다.
Schema.rb
create_table "itemizeds", force: :cascade do |t|
t.integer "item_id", limit: 4
t.integer "order_id", limit: 4
t.integer "quantity", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "title", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "orders", force: :cascade do |t|
t.integer "customer_id", limit: 4
t.integer "store_id", limit: 4
t.integer "order_id", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "price", precision: 8, scale: 2
t.decimal "discount", precision: 8, scale: 2
end
Order.rb (모델)
class Order < ActiveRecord::Base
has_many :itemized
has_many :items, :through => :itemized
end
Item.rb (모델)
class Item < ActiveRecord::Base
has_many :itemized
has_many :orders, :through => :itemized
end
Itemized.rb (모델)
class Itemized < ActiveRecord::Base
belongs_to :item
belongs_to :order
end
방해가되는지 확실하지 않지만 매장 모델도 있고 매장에는 주문이 많습니다.
귀하의 도움과 시간에 감사드립니다!
http://guides.rubyonrails.org/association_basics.html 에서 다 대다 연관을 살펴보면
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
그래서 나는 그것이 복수화의 문제라고 생각합니다. :through => :itemizeds
대신 시도하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다