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?
user.teamsrefers to the whole collection of teams the user might belong to. Sonameisn't the name of a team, but might be picking up thenamemethod of the class (which isTeam). You need to show a specific team by selecting a team fromuser.teams(e.g.,first_team = user.teams.first) then show its name (e.g.,first_team.name).