Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:12:05+00:00 2026-06-14T18:12:05+00:00

Here my models: class User < ActiveRecord::Base has_many :bookmarks end class Topic < ActiveRecord::Base

  • 0

Here my models:

class User < ActiveRecord::Base
  has_many :bookmarks
end

class Topic < ActiveRecord::Base
  has_many :bookmarks
end

class Bookmark < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic
  attr_accessible :position
  validates_uniqueness_of :user_id, :scope => :topic_id
end

I want to fetch all topics with, for current_user, the associated bookmark. ATM, I do:

Topic.all.each do |t|
    bookmark = t.bookmarks.where(user_id: current_user.id).last
    puts bookmark.position if bookmark
    puts t.name
end

This is ugly and do too much DB queries. I would like something like this:

class Topic < ActiveRecord::Base
  has_one :bookmark, :conditions => lambda {|u| "bookmarks.user_id = #{u.id}"}
end

Topic.includes(:bookmark, current_user).all.each do |t| # this must also includes topics without bookmark
    puts t.bookmark.position if t.bookmark
    puts t.name
end

Is this possible? Have I any alternative?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:12:06+00:00Added an answer on June 14, 2026 at 6:12 pm

    *Hmm I’m not sure I understood your question but this may help you:

    # code
    class Topic < ActiveRecord::Base
      scope :for_user, lambda { |user| includes(:bookmarks).where(bookmarks: { user_id: user.try(:id) || user} ) }
    
    # call
    Topic.for_user(current_user) # => Array of Topics
    

    As you can see the parameter of the scope for_user can be a User object OR a user id.

    Hope this helps!

    Similar questions:

    • How to query a model based on attribute of another model which belongs to the first model?
    • Rails active record querying association with 'exists'
    • Rails 4 scope to find parents with no children
    • Join multiple tables with active records
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are my models: class BillingProfile < ActiveRecord::Base belongs_to :student attr_accessible :cost end class
So I have two models here: class Screen < ActiveRecord::Base belongs_to :user validates :screen_size,
My code: class User < ActiveRecord::Base belongs_to :university end class University < ActiveRecord::Base has_many
Hey, need a little help here. I have two models: class User < ActiveRecord::Base
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I defined these 3 models in Rails3. class User < ActiveRecord::Base has_many :questions has_many
So I have the following models in my program: class User < ActiveRecord::Base has_many
Here's my user model: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
I have my models & associations currently setup like so: class User < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.