Rails 3, has_one / has_many with lambda condition

*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:

Leave a Comment