On 11 July 2014 22:26, Jan Yo <li...@ruby-forum.com> wrote:
> 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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsA9B5VyVXUpnSNkJ8ZNY0Nb3MWFFS1%3DN2oxh7JtdJxVA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to