Company has many Departments,
Department has many Employees.
Is there nice easy way to select all Employees of certain Company? I
mean using just proxy objects, without joins.
Company has many Departments,
Department has many Employees.
Is there nice easy way to select all Employees of certain Company? I
mean using just proxy objects, without joins.
In your model:
app/models/company.rb
has_many :departments
has_many :employees, :through => :departments
And then reference via an existing company object:
@company.employees
Thanks, Ryan, you hacked my case In fact, I have four objects, also
Holding has many Companies.
Can I in the same way select all employees of Holding? I read manual
about join models in Rails and did not find words about joining
through two or more entities.
It’s not wise to go through four levels… but if you must. Define a
method in your holding model:
def employees
companies.map { |c| c.employees }
end
Yes, I used such code as my first solution (just don’t forget to
call .flatten) and now looking for something nicer. Thank you anyway,
Ryan.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs