Created: 2008-07-19 14:42
Updated: 2017-05-03 05:59
License: mit




  1. Install the plugin with script/plugin install git://github.com/fnando/has_bookmarks.git

  2. Generate a migration with script/generate migration create_bookmarks and add the following code:

    class CreateBookmarks < ActiveRecord::Migration def self.up create_table :bookmarks do |t| t.references :bookmarkable, :polymorphic => true t.references :user t.string :name t.timestamps end

     add_index :bookmarks, :bookmarkable_id
     add_index :bookmarks, :bookmarkable_type
     add_index :bookmarks, :user_id
     add_index :bookmarks, :name


    def self.down drop_table :bookmarks end end

  3. Run the migrations with rake db:migrate


  1. Add the method call has_bookmarks to your model.

    class Product < ActiveRecord::Base has_bookmarks end

  2. Add this association on your User model:

    class User < ActiveRecord::Base has_many :bookmarks, :dependent => :destroy end

    product = Product.first user = User.first

    product.bookmark(:user => user) # => product.bookmarks # => [] product.bookmarked?(:user => user) # => true product.find_users_that_bookmarked # => [] product.find_bookmark_by_user(:user => user) # => [] product.remove_bookmark_for(:user => user)

If you have different types of bookmarks, you can use the option :name:

product.bookmark(:user => user, :name => 'wishlist')
product.bookmark(:user => user, :name => 'i_have')

product.bookmarked?(:user => user, :name => 'wishlist')
product.find_users_that_bookmarked(:name => 'wishlist')
product.find_bookmark_by_user(:user => user, :name => 'wishlist')
product.remove_bookmark_for(:user => user, :name => 'wishlist')

To retrieve bookmarks from an user:


If you have has_paginate installed, you can paginate the users that bookmarked a given item:

product.find_users_that_bookmarked(:page => 2)

Optionally, you can have counter columns for general/named bookmarks. If your model doesn't have counter columns, nothing will happen.

product.bookmark(:user => user) # => update bookmarks_count
product.bookmark(:user => user, :name => 'wishlist') # => update wishlist_bookmarks_count

NOTE: You should have a User model. Otherwise, this won't work!

Copyright (c) 2008 Nando Vieira, released under the MIT license

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more