0
以下應該如何模型協會模型協會所屬,有
User can purchase many Items
Items can be purchased by many users
Items have many categories
以下應該如何模型協會模型協會所屬,有
User can purchase many Items
Items can be purchased by many users
Items have many categories
的Active Record Associations文件似乎言自明!
你需要Item
和Category
之間的關係has_many
和User
之間Item
一個has_many...through
關係,。
# app/models/user.rb
class User < ActiveRecord::Base
has_many :purchases
has_many :items, through: :purchases
end
# app/models/item.rb
class Item < ActiveRecord::Base
has_many :purchases
has_many :users, through: :purchases
has_many :categories
end
# app/models/purchase.rb
class Purchase < ActiveRecord::Base
belongs_to :user
belongs_to :item
end
# app/models/category.rb
class Category < ActiveRecord::Base
belongs_to :item
end
感謝您的幫助 –