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_id
s 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
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.
The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns. Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id , order_id ).
This was useful for me:
Model.where(...) | Model.where(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With