Zelenka Zelenka - 3 months ago 7
Ruby Question

Optimize long chain of associations

I have a Customer that goes to the Shop which has a Manager. Both Customer and Manager are from People model, different roles only.

What is the best way to get a Manager from Customer?

Right now I have this solution which is very slow:

shop_manager = customer.shop.people.select{|p| p.has_role?(:manager)}.first


person.rb:

belongs_to :shop


shop.rb:

has_many :people


Roles are assigned using rolify gem.

Answer

Like this?

shop_manager = customer.shop.people.with_role(:manger).first
Comments