acts_as_favorite
We are using Josh Martin’s acts_as_favorite plugin (also here).
I have made a few acts_as_favorite bug fixes (contextual diff file). These properly set the user_id in your favorites table and update the favorites associations as you add/delete favorites.
Here is how we tell the user class that it will have favorites:
class User < ActiveRecord::Base acts_as_favorite_user end
Here is how we tell a different model that it can be marked as a favorite:
class Thing < ActiveRecord::Base acts_as_favorite end
In our controller, we allow a user to mark a favorite thing. There is code elsewhere which makes sure that the user is logged in.
class ThingController < ApplicationController
def add_favorite
@thing = Thing.find(params[:id])
@user = User.find(session[:user_id])
@user.has_favorite(@thing)
redirect_to :action => ’show’, :id => @thing
end
def remove_favorite
@thing = Thing.find(params[:id])
@user = User.find(session[:user_id])
@user.has_no_favorite(@thing)
redirect_to :action => ’show’, :id => @thing
end
end
That’s all there is to it. You now have users who can mark objects as favorites.
Update: It appears that the official repository for acts_as_favorite is not reliable. I have archived my snapshot of acts_as_favorite. This version includes the patch listed above.
13 Comments so far
Leave a reply


Hello, thanks for the tutorial. I am looking for the acts_as_favorite plugin, but it is no where to be found? Where can I get a copy? I would love to try out your tutorial. Thanks in advance.
@lambo4jos: Unfortunately, it appears that the primary source for acts_as_favorite is no longer available. I will make a version available over the next few days until the official repository becomes available again.
Thank you much. I’m sure you know, but the only other option, and one I don’t want to work with is the acts_as_bookmarkable. Maybe that would fit the needs of someone else. I would love to play with acts_as_favorites and get it working by the end of the week. hehe. :) Being this is rails, and because of your simple tutorial, it would probably only take a few minutes. I love this stuff.
Hello. was just writing to see if you were able to package up the acts_as_favorite plugin? I am still interested in trying it out. Thanks in advance.
Sorry for all the comments, but it seems the repository is now working so i just grabbed it. Thanks again for the tutorial.
Thanks for this how-to. As per your instructions, I installed via
svn export http://svn.webwideconsulting.com/svn/acts_as_favorite vendor/plugins/acts_as_favorite
Also, I noticed this version already has your patch installed.
One more thing, FYI:
The User model only needs the ‘acts_as_favorite_user’ in order to be favorite-able itself. That’s nice and simple. I just thought I’d make it clear.
Hello, it seems the original source for the plugin is down, for a long time now. Can anyone provide me with another way to get the plugin? Maybe a zipped version, or another repository… Thanks in advance.
Just for the record I renamed “favorite_users” in the ats_as_favorite module ( in acts_as_favorite.rb ) to “favored_by” so you can use favorite users , otherwise it does a reverse lookup and thats not what you want necessarily .
So now you have the ablity to do :
class User
eh my code got cut off…
class User [inward bracket] ActiveRecord::Base
acts_as_favorite_user
acts_as_favorite
end
It seems original source has moved here:
http://svn.webwideconsulting.com/plugins/acts_as_favorite/
Glad to hear the plugin is getting some use. If you want to send the patch over to my email provided there, I’ll look it over and apply it.
I’ve got a problem with the dynamic “.favorite_blahs” methods provided by the Identity mixin. Can anyone help?
I have Lessons and Resources. Resource is extended by TeachingObject and LearningObject.
Lessons seem to work fine:
@user.has_favorite(@lesson)
@user.favorite_lessons
=> [@lesson]
but:
@to = a TeachingObject object
@user.has_favorite(@to)
@user.favorite_teaching_objects
=> []
@user.favorite_resources
=> []
Can anyone think of why i can’t get at my favorite resources/teachingobjects?
thanks
max