Refactor Link Table relationsip

I need some code refactored as it is very ugly.

   memberships = customer.companies.collect(&:name).sort().join(',')

  for membership in customer.memberships           membership.membership_roles.each do |membership_role|             cust_roles << membership_role.role.name + ", "           end    end

Here's the active record relation:

customer has many memberships membership_role belongs to membership and role (link table)

Is there a more compact, cleaner way to write this?

Specify membership has_many roles through membership_role, then you can use membership.roles, which helps a bit.

Colin

Colin Law wrote in post #1152183:

With has_many through then you can use collect and join on membership.roles

Colin