You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置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!

Step 1: Define the Bookmark Join Model

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
Step 2: Configure the User Model

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
Step 3: Configure the Post Model

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
Using the Associations (Including Your Requested 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:

  1. 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.

  2. Use the shortcut association (cleaner & more flexible):
    If you added the bookmarked_posts association in the User model, you can skip the iteration and use:

    current_user.bookmarked_posts
    

    This 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!

Why This Works
  • The belongs_to associations in Bookmark create the direct links between each bookmark and its parent User/Post.
  • The has_many associations 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:42:10