Reputation: 853
I am making a model where users can belong to multiple teams and teams have multiple people. I have checkboxes but they don't pass the value onto the object.
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :teams
accepts_nested_attributes_for :teams
end
class Team < ActiveRecord::Base
has_many :users
attr_accessible :name
end
Here is the code in my controller
def create
@users = User.all
@user = User.new
@teams = Team.all
@user.attributes = {:teams => []}.merge(params[:user] || {})
end
Here is the code in my view file
<%= form_for @user, url: {action: "create"} do |f| %>
<%= f.label :teams%>
<% for team in @teams %>
<%= check_box_tag team.name, team.name, false, :teams => team.name%>
<%= team.name -%>
<% end %>
<%= submit_tag "Create User" %>
I am trying to show it into
<%= user.teams.name %>
But the only output is "Team" Can someone tell me what I am doing wrong?
Upvotes: 0
Views: 173
Reputation: 36860
Actually, you can't do a many-to-many relationship that way... you need to do has_many :through
or alternatively has_and_belongs_to_many
Nice explanation here...
http://guides.rubyonrails.org/association_basics.html
Upvotes: 1