Nadiya
Nadiya

Reputation: 1527

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.

Upvotes: 0

Views: 54

Answers (1)

lusketeer
lusketeer

Reputation: 1930

Like this?

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

Upvotes: 3

Related Questions