Rails 4 - Many to many relationship

10,863

Solution 1

Applicable for => Rails 4:

app/model/Bookmark.rb

class Bookmark < ActiveRecord::Base
  has_and_belongs_to_many :lists
end

app/model/List.rb

class List < ActiveRecord::Base
  has_and_belongs_to_many :bookmarks
end

create a new migration

rails generate migration CreateJoinTableListBookmark List Bookmark

Migrate

rake db:migrate

Solution 2

Haven't tested it, but this is how it works

class Bookmark < ActiveRecord::Base
    has_and_belongs_to_many :bookmark_lists
end

class BookmarkList < ActiveRecord::Base
    has_and_belongs_to_many :bookmarks
end

And you will need to have migrations for each and a migration for a bridge-table (I'm assuming you want a title for a bookmark and for the lists):

rails generate model Bookmark title
rails generate model BookmarkList title
rails generate model BookmarkListsBookmarks bookmark_list_id:integer bookmark_id:integer

Not sure if rails wants BookmarkListsBookmarks or BookmarksBookmarkLists if it throws an error just try the other one.

Solution 3

There are two model Bookmark and List.

  1. A List can have many bookmarks
  2. A Bookmark can have many lists

It is case of Many to Many relationship.

To resolve this problem, there will be introduce one more Model(ListBookMark) to solve many to many relationship.

ListBookmark attributes:

list_id bookmark_id

there may be more attribute according to requirements.

class List < ActiveRecord::Base
    attr_accessible :title
    has_many :list_bookmarks
    has_many :bookmarks, through: :list_bookmarks
end

class ListBookmark < ActiveRecord::Base
    attr_accessible :bookmark_id, :list_id
    belongs_to :list
    belongs_to :bookmark
end

class Bookmark < ActiveRecord::Base
    attr_accessible :title
    has_many :list_bookmarks
    has_many :lists, through: :list_bookmarks
end

I think that It will helpful to solve this problem.

To read about relationship click here

enter image description here

Share:
10,863
PrivateUser
Author by

PrivateUser

Updated on June 05, 2022

Comments

  • PrivateUser
    PrivateUser almost 2 years

    I have a Bookmark model. I would like to have a List model. So my users can create Bookmark lists.

    I've created a List scaffold with this command

    rails generate scaffold List title:string
    
    1. A List can have many bookmarks
    2. A Bookmark can have many lists

    Can someone help me to create List Bookmark relationship.?

    It would be awesome if you can give me some resources to learn.

    Update:

    1. A Bookmark can have many lists

    Lets say I bookmarked http://stackoverflow.com. And say I have two lists like:

    1. Programming Help
    2. Favorite sites

    Then I should be able to add my bookmark to both lists.

    So I guess A Bookmark can have many lists is a valid statement.

  • PrivateUser
    PrivateUser about 10 years
    Thanks for your answer. But I couldn't find has_many_and_belongs_to keyword in rails 4 documentation. Are you sure rails 4 uses that? Please check this page. guides.rubyonrails.org/association_basics.html
  • Zain Zafar
    Zain Zafar over 8 years
    I have followed your approch. I am wondering now how I can associate multiple lists to multiple bookmarks?
  • user000001
    user000001 over 6 years
    In rails 5 it seems that lowercase plural model names should be user in the generate command. It would be rails generate migration CreateJoinTableListBookmark lists bookmarks