I need to write a scope that I will pass a user’s id through and will collect all the user’s list associates with all the companies for that user from the users table.
In User.rb:
has_many :employments
has_many :companies, :through => :employments, :dependent => :destroy
...
In Employment.rb:
belongs_to :user
belongs_to :company
In Company.rb:
has_many :employments
has_many :users, :through => :employments, :dependent => :destroy
This could be possible using something like:
current_user.companies.each{|c| c.users.each {|u| u}}
but writing like this I think is much more time consuming.
The following (not tested) scope finds all companies where a given user had work.
Run
rails cand execute tryCompany.with_user(User.last!.id)and see what happens.