如何设置Rails的User、Post、Bookmark模型关联以支持current_user.bookmarks.posts调用
Hey there! Let's break down how to set up these model associations so you can achieve the behavior you're looking for. I'll assume you're using Ruby on Rails with Active Record (the most common setup for this kind of relationship)—if you're working with a different ORM, just let me know and I can adjust the examples!
Your Bookmark model acts as the bridge between User and Post, so it needs foreign keys linking to both models (make sure your bookmarks database table has user_id and post_id integer columns).
In app/models/bookmark.rb:
class Bookmark < ApplicationRecord # Each bookmark belongs to exactly one user belongs_to :user # Each bookmark belongs to exactly one post belongs_to :post end
A user can have multiple bookmarks, so we'll set up a one-to-many association here. I'll also add a handy shortcut to directly access bookmarked posts (more on that later):
In app/models/user.rb:
class User < ApplicationRecord # A user owns many bookmarks (delete bookmarks if the user is deleted) has_many :bookmarks, dependent: :destroy # Optional: Directly fetch all posts the user has bookmarked has_many :bookmarked_posts, through: :bookmarks, source: :post end
A post can be bookmarked by many users, so we'll mirror the association here:
In app/models/post.rb:
class Post < ApplicationRecord # A post has many bookmarks (delete bookmarks if the post is deleted) has_many :bookmarks, dependent: :destroy # Optional: Directly fetch all users who bookmarked this post has_many :bookmarkers, through: :bookmarks, source: :user end
current_user.bookmarks.posts) Quick clarification: current_user.bookmarks returns a collection of Bookmark objects, not a single bookmark. That means you can't call .posts directly on the collection. Instead, here's how to get the associated posts:
Fetch posts via the bookmark collection:
To get all posts from the user's bookmarks, use this to iterate over the collection:current_user.bookmarks.map(&:post)This gives you an array of Post objects the user has bookmarked.
Use the shortcut association (cleaner & more flexible):
If you added thebookmarked_postsassociation in the User model, you can skip the iteration and use:current_user.bookmarked_postsThis returns an Active Record relation, so you can chain additional queries (like
.where(published: true)or.order(created_at: :desc)) directly—way more powerful than a plain array!
- The
belongs_toassociations in Bookmark create the direct links between each bookmark and its parent User/Post. - The
has_manyassociations in User/Post let you fetch all related bookmarks for a given user or post. - The
has_many ... through:association creates a direct, efficient link between User and Post via the Bookmark join model, avoiding extra iteration when you just want the posts.
Just double-check your bookmarks table schema includes user_id, post_id, and timestamps (optional but recommended) to make everything work smoothly.
内容的提问来源于stack exchange,提问作者Vishal Goel

