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
Can someone help me to create List Bookmark relationship.?
It would be awesome if you can give me some resources to learn.
- A Bookmark can have many lists
Lets say I bookmarked http://stackoverflow.com
. And say I have two lists like:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With