Zelenka Zelenka - 9 months ago 25
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


belongs_to :shop


has_many :people

Roles are assigned using rolify gem.


Like this?

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