【发布时间】:2012-01-28 04:01:24
【问题描述】:
我需要编写一个范围,我将通过该范围传递用户的 ID,并从 users 表中收集与该用户的所有公司相关联的所有用户列表。
在 User.rb 中:
has_many :employments
has_many :companies, :through => :employments, :dependent => :destroy
...
在就业.rb:
belongs_to :user
belongs_to :company
在 Company.rb 中:
has_many :employments
has_many :users, :through => :employments, :dependent => :destroy
这可以通过以下方式实现:
current_user.companies.each{|c| c.users.each {|u| u}}
但我认为这样写更费时间。
【问题讨论】:
-
如果执行 User.last!.companies 会发生什么?
标签: ruby-on-rails scope many-to-many