I'm trying to create a simple many-to-many association for my Artist
model. I've tried following the example here but for some reason its just not translating for my particular project. I basically just want users to belong to groups and reflect their memberships in my show view.
<!-- Shows the list of groups an artist belongs to -->
<% if @artist.groups.any? %>
<%= @artist.groups.each do |group| %>
<%= group.name %>
<% end %>
<% end %>
<!-- Shows the members of the groups -->
<% if @artist.members.any? %>
<p>
<b>Members:</b>
<% @artist.members.each do |member| %>
<%= member.name %>
<% end %>
</p>
<% end %>
I currently have my User model setup with a has_one association and works great. Now I just need to make it into a many_to many association to be able to call @artist.groups
. vs. @artist.group
. Would a a join table now be necessary to get this to work?
class Artist < ActiveRecord::Base
attr_accessor :group_id
has_many :members, class_name: 'Artist', foreign_key: 'group_id'
belongs_to :group, class_name: 'Artist', foreign_key: 'group_id'
end