views:

62

answers:

1

I currently have two active record queries that I would like to combine together

joins("join relationships ON user_id = followed_id").
              where("follower_id = #{user.id}")

and

where(:user_id => user.id)

Basically I want the results of the second one to appear with the first similar to a UNION statement in SQL. Can it be done in ActiveRecord in this way?

I would prefer to use a union rather that have to join all the followed_ids in a string and use the IN clause in sql.

Any ideas?

-----Edit------ I am looking for a way to get this to work with lazy loading

+2  A: 

Use relation & relation:

Model.joins("join relationships ON user_id = followed_id").where("follower_id = {user.id}") & Model.where(:user_id => user.id)
fantactuka
This forces ActiveRecord to put the results in an array eagerly. Can this be done with lazy loading?
Zaid Zawaideh
btw.. I did vote up your answer since it got me there part of the way.
Zaid Zawaideh