Zelenka Zelenka - 1 year ago 51
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.

Answer Source

Like this?

shop_manager = customer.shop.people.with_role(:manger).first
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download