[英]Rails has_many :through, undefined method
在Rails中的多对多关系中遇到麻烦。 我有三种模式:
订单中逐项列出了许多项目,反之亦然。
查询Item.find(1).orders很好,但是当我尝试Order.find(1).items时,它返回:
NoMethodError: undefined method `items' for #<Order:0x007fcad3bb3258>
这是我的代码:
模式.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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.