Ruby on rails 根据特定用户的订单查找页面
我有以下协会:Ruby on rails 根据特定用户的订单查找页面,ruby-on-rails,associations,Ruby On Rails,Associations,我有以下协会: User has_many Order Order has_many Pages 如何找到属于特定用户的所有页面?不是直接关联,但表顺序在它们之间。所以,我不能做: user.pages 我只需要做user.orders您想要的可能是不同的关联。尝试一下has\u many:通过联想 User.rb has_many :orders has_many :pages, through: :orders Order.rb belongs_to :user belongs_to
User has_many Order
Order has_many Pages
如何找到属于特定用户的所有页面?不是直接关联,但表顺序在它们之间。所以,我不能做:
user.pages
我只需要做
user.orders
您想要的可能是不同的关联。尝试一下has\u many:通过联想
User.rb
has_many :orders
has_many :pages, through: :orders
Order.rb
belongs_to :user
belongs_to :page
Page.rb
has_many :orders
has_many :users, through: :orders
有关更多信息,请参阅Rails文档:
如果您真的不需要加入模型上的更多元数据(在本例中为Order.rb),您也可以简单地使用HABTM关联
User.rb
has_many :orders
has_many :pages, through: :orders
Order.rb
belongs_to :user
belongs_to :page
Page.rb
has_many :orders
has_many :users, through: :orders
- 生成一个名为users\u pages的表
User.rb
has_and_belongs_to_many :pages
Pages.rb
has_and_belongs_to_many :users
有关此关联的更多信息,请阅读:
有关何时选择其中一种的更多信息,请阅读:Hommer,我的回答有帮助吗?