ActiveRecord::AssociationTypeMismatch in UsersController#update

Hi
Im trying to link my User model to a Teams model. I seem to be getting
the following errors. Any help would be great as Im just new to RoR
Thanks

error
ActiveRecord::AssociationTypeMismatch in UsersController#update

Team(#2183395560) expected, got String(#2174675960)
app/controllers/users_controller.rb:67:in `update'
app/controllers/users_controller.rb:66:in `update'

users_controller.rb
# PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to(users_url,
                :notice => "The information for #{@user.first_name}
#{@user.last_name} was successfully updated.") }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @user.errors, :status =>
:unprocessable_entity }
      end
    end
  end

user.rb
class User < ActiveRecord::Base
  belongs_to :team
  attr_accessible :team

team.rb
class Team < ActiveRecord::Base
  has_many :users
  has_many :schedules
  attr_accessible :name
end

user/_form.html.erb
<div class="_25">
      <p>
              <%= f.label :team %>
              <%= select("user", "team", Team.all.collect { |p| [p.name,
p.id] }, {:include_blank => 'None'}) %>
      </p>
    </div>

Hi
Im trying to link my User model to a Teams model. I seem to be getting
the following errors. Any help would be great as Im just new to RoR
Thanks

error
ActiveRecord::AssociationTypeMismatch in UsersController#update

Team(#2183395560) expected, got String(#2174675960)
app/controllers/users_controller.rb:67:in `update'
app/controllers/users_controller.rb:66:in `update'

Perhaps it would be useful if you told us which is line 66.

users_controller.rb
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])

In case I have made a lucky guess and it is this line then look in
development.log to see what params[:user] will be. Also have a look
at the Rails Guide on debugging to see techniques that can be used to
debug your code.

Colin

http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html

I gues it’s raise because you don’t assign object. You should change it this way:

<%= select(“user”, “team_id”, Team.all.collect { |p| [p.name,
p.id] }, {:include_blank => ‘None’}) %>

Hi
Thanks for the help, i managed to get this to work with the following
line

<%= f.select("team_id", Team.all.collect { |p| [p.name, p.id] }, {
:include_blank => 'None' }) %>

though in my view/users/index
when i call the following <%= user.team %> i am getting this displayed
#<Team:0x10532a4f0>, i want it to display what team the player has been
allocated to

in my view/teams/show if i call the following
<% for player in @team.users %><%= player.first_name %> <%=
player.last_name %>
i can see what players are loaded for a particular team

thanks

That (#<Team:0x10532a4f0>) *is* the team - a Team *object*.

Maybe you actually want to display an *attribute* of the object? Like
says its name? Look at your own 'player' example.

hi hassan
i have tried something similar and got this

NoMethodError in Users#index

Showing
/Users/*****/Sites/rails_projects/*****/app/views/users/index.html.erb
where line #67 raised:

undefined method `teams' for nil:NilClass
Extracted source (around line #67):

64: <td
65: <% if session[:user_id] == user.id %>
class="active_cell"
66: <% end %>>
67: <% for team in @user.teams
68: team.name
69: end %>
70:

i have tried something similar and got this

? That's not remotely similar to your original example:

when i call the following <%= user.team %> i am getting this displayed

Doesn't it seem apparent that if this <%= user.team %> shows you the
team object, <%= user.team.name %> would show you the e.g. name
attribute of that object?

Thanks Hassan, I now have a better understanding of what you mean