How to combine two scopes with OR

Answering my own question. I think I figured out a way.

where("pages.id IN (?) OR pages.id IN (?)",
  Page.where(
      "user_id IN (?) OR user_id = ?",
      user.watched_ids, user.id
  ),
  Page
    .joins(:attachments)
    .where("attachments.tag_id IN (?)", user.tags)
    .select("DISTINCT pages.*")
)

It seems to be working so far, hope this is it!


There's a rails pull request for this feature (https://github.com/rails/rails/pull/9052), but in the meantime, some one has created a monkey patch that you can include in your initializers that will allow you to or scopes and where clauses in one query and still give you an ActiveRecord::Relation:

https://gist.github.com/j-mcnally/250eaaceef234dd8971b

With that, you'd be able to OR your scopes like this

Post.tagfeed(current_user).or.friendfeed(current_user)

or write a new scope

scope :mother_of_all_feed, lambda{|user| tagfeed(user).or.friendfeed(user)}