Combine two ActiveRecord::Relation objects

天涯浪子 提交于 2019-11-26 04:36:47

问题


Suppose I have the following two objects:

first_name_relation = User.where(:first_name => \'Tobias\') # ActiveRecord::Relation
last_name_relation  = User.where(:last_name  => \'Fünke\') # ActiveRecord::Relation

is it possible to combine the two relations to produce one ActiveRecord::Relation object containing both conditions?

Note: I\'m aware that I can chain the wheres to get this behavior, what I\'m really interested in is the case where I have two separate ActiveRecord::Relation objects.


回答1:


If you want to combine using AND (intersection), use merge:

first_name_relation.merge(last_name_relation)

If you want to combine using OR (union), use or:

first_name_relation.or(last_name_relation)

Only in ActiveRecord 5+; for 4.2 install the where-or backport.




回答2:


Relation objects can be converted to arrays. This negates being able to use any ActiveRecord methods on them afterwards, but I didn't need to. I did this:

name_relation = first_name_relation + last_name_relation

Ruby 1.9, rails 3.2




回答3:


merge actually doesn't work like OR. It's simply intersection (AND)

I struggled with this problem to combine to ActiveRecord::Relation objects into one and I didn't found any working solution for me.

Instead of searching for right method creating an union from these two sets, I focused on algebra of sets. You can do it in different way using De Morgan's law

ActiveRecord provides merge method (AND) and also you can use not method or none_of (NOT).

search.where.none_of(search.where.not(id: ids_to_exclude).merge(search.where.not("title ILIKE ?", "%#{query}%")))

You have here (A u B)' = A' ^ B'

UPDATE: The solution above is good for more complex cases. In your case smth like that will be enough:

User.where('first_name LIKE ? OR last_name LIKE ?', 'Tobias', 'Fünke')



回答4:


I've been able to accomplish this, even in many odd situations, by using Rails' built-in Arel.

User.where(
  User.arel_table[:first_name].eq('Tobias').or(
    User.arel_table[:last_name].eq('Fünke')
  )
)

This merges both ActiveRecord relations by using Arel's or.


Merge, as was suggested here, didn't work for me. It dropped the 2nd set of relation objects from the results.




回答5:


There is a gem called active_record_union that might be what you are looking for.

It's example usages is the following:

current_user.posts.union(Post.published)
current_user.posts.union(Post.published).where(id: [6, 7])
current_user.posts.union("published_at < ?", Time.now)
user_1.posts.union(user_2.posts).union(Post.published)
user_1.posts.union_all(user_2.posts)



回答6:


This is how I've "handled" it if you use pluck to get an identifier for each of the records, join the arrays together and then finally do a query for those joined ids:

  transaction_ids = @club.type_a_trans.pluck(:id) + @club.type_b_transactions.pluck(:id) + @club.type_c_transactions.pluck(:id)
  @transactions = Transaction.where(id: transaction_ids).limit(100)



回答7:


If you have an array of activerecord relations and want to merge them all, you can do

array.inject(:merge)



回答8:


Brute force it:

first_name_relation = User.where(:first_name => 'Tobias') # ActiveRecord::Relation
last_name_relation  = User.where(:last_name  => 'Fünke') # ActiveRecord::Relation

all_name_relations = User.none
first_name_relation.each do |ar|
  all_name_relations.new(ar)
end
last_name_relation.each do |ar|
  all_name_relations.new(ar)
end



回答9:


I've stumbled upon this issue a handful of times and the generally accepted responses never quite worked for me.

The proposed solution in the accepted answer seemed like it would work best:

first_name_relation.or(last_name_relation)

But I could never get this to work and would always receive an 'undefined method' error for 'or'.

My solution:

combined_relation = first_name_relation + (last_name_relation - first_name_relation)

This gives me essentially the results of a UNION between the original queries and the relation objects are still the correct ActiveRecord objects.

This is similar to the answer provided by @thatmiddleway, though I needed to add the subtraction to get a DISTINCT collection of objects. This does convert the combined collection to an ARRAY, but I could still call model methods on on the individual instances in an 'each' iterator in my view, which solved my issue.

This is my first StackOverflow answer (gasp!), so I welcome your feedback!



来源:https://stackoverflow.com/questions/9540801/combine-two-activerecordrelation-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!