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?

On 11 July 2014 22:26, Jan Yo [email protected] wrote:

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 L. wrote in post #1152183:

On 11 July 2014 22:26, Jan Yo [email protected] wrote:

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

I was looking for better, more compact code after the for statement.

On 11 July 2014 23:18, Jan Yo [email protected] wrote:

can use membership.roles, which helps a bit.

Colin

I was looking for better, more compact code after the for statement.

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

Colin