Trying to get acts_as_taggable plugin to work with users! :(

Hey all, this is my plea for help, I’ve been trying to figure this out
for a day and a half without avail, please gurus at least cast your eye
upon it and try to help me out :slight_smile:

Ok, what I’m trying to do is get the acts_as_taggable plugin to work
with many users. I’ve got a picture and a user model to work with. I can
add tags to pictures fine, but when I start trying to add models it
starts to fail.

The code that I’ve been following is at :

It starts at: “Sutch is right…”

I’ve changed my plugin/code to reflect the changes indicated by foo, so
I know that code is correct. The code that I’m using in
pictures_controller is

def create
@picture = Picture.create! params[:picture]
@picture.tag_with(params[:tag_list], current_user)

redirect_to :action => 'show', :id => @picture
    rescue ActiveRecord::RecordInvalid
render :action => 'new'


which draws in from a form in a view of new.rhtml.

The error that I get is that;

" ActiveRecord::AssociationTypeMismatch in PicturesController#create

User expected, got User "

Strangely though, this error only happens on the 2nd and subsequent
times the request is run. ie: if i restart my server, it’ll allow me to
add one picture with the user_id filled in the taggigns table, but when
I go to create a new one then the error occurs.

Thank you so much for any light you can shine on the problem :slight_smile:

Steven Hann


I encountered similar issue today. It took me a while to find out what
went wrong.


The reason is because you store user object in session scope, and
‘current_user’ returns an old copy of ‘User.class’.

In development environment, ‘User.class’ is loaded and changes for every
request. On the other hand ActiveRecord caches ‘User.class’ for
TypeMismatch check.

Thus for subsequent requests check failed, because the User.class
returned by ‘current_user’ is a different instance compared to the cache
copy used by ActiveRecord.

Note that this issue will not happen in production environment, because
the model classes are not reloaded for every request.


The workaround if either storing just the user_id in the session and
reload the user object on every request.

Another way is to add add a conditional statement to reload user object
only if ENV[‘RAILS_ENV’] == ‘development’.

Hope it helps.



Sorry ignore about the solution that I mentioned earlier it doesn’t

Instead, update tag.rb as such, note the usage of :user_id instead of

  def on(taggable, user)
    taggings.create :taggable => taggable, :user_id =>