PrivateUser
PrivateUser

Reputation: 4524

Rails 4 - Many to many relationship

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.

Upvotes: 5

Views: 10957

Answers (4)

PrivateUser
PrivateUser

Reputation: 4524

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

Upvotes: 35

user3040683
user3040683

Reputation:

I think that there is many to many relationship.

for solving this problem. A new model ListBookmark Will be created.

ListBookmark basic attributes: list_id bookmark_id

according to requirement more attribute may be added.

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

Upvotes: 2

uma
uma

Reputation: 2952

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

Upvotes: 1

brancz
brancz

Reputation: 451

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.

Upvotes: 2

Related Questions