ActiveRecord::AssociationTypeMismatch in UsersController#update

48 views
Skip to first unread message

Paul McGuane

unread,
Jun 8, 2012, 10:36:36 PM6/8/12
to rubyonra...@googlegroups.com
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>

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jun 9, 2012, 3:51:25 AM6/9/12
to rubyonra...@googlegroups.com
On 9 June 2012 03:36, Paul McGuane <li...@ruby-forum.com> wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

Андрей Большов

unread,
Jun 9, 2012, 10:40:24 AM6/9/12
to rubyonra...@googlegroups.com
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'}) %>  


суббота, 9 июня 2012 г., 6:36:36 UTC+4 пользователь Ruby-Forum.com User написал:

Paul McGuane

unread,
Jun 9, 2012, 10:47:35 AM6/9/12
to rubyonra...@googlegroups.com
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

Hassan Schroeder

unread,
Jun 9, 2012, 10:57:28 AM6/9/12
to rubyonra...@googlegroups.com
On Sat, Jun 9, 2012 at 7:47 AM, Paul McGuane <li...@ruby-forum.com> wrote:

> 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

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.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Paul McGuane

unread,
Jun 9, 2012, 11:05:13 AM6/9/12
to rubyonra...@googlegroups.com
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:

Hassan Schroeder

unread,
Jun 9, 2012, 11:10:58 AM6/9/12
to rubyonra...@googlegroups.com
On Sat, Jun 9, 2012 at 8:05 AM, Paul McGuane <li...@ruby-forum.com> wrote:

> 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?

Paul McGuane

unread,
Jun 9, 2012, 11:15:52 AM6/9/12
to rubyonra...@googlegroups.com
Thanks Hassan, I now have a better understanding of what you mean
Reply all
Reply to author
Forward
0 new messages