1

I have two sets of active records

users_by_type = User.where(type: 'customer')

Class of this is : users_by_type.class => Active Record Relation

users_by_tag = User.tagged_with('admin')

Class of this is : users_by_tag => ActiveRecord Relation

When I combine both sets like:

users = users_by_type + users_by_tag
or
users = users_by_type.merge(users_by_tag)

its give result as Array but I want it as Active Record Relation.

Class of users is: Array

Because in array, I am unable to use where clause and group clause.

How can I combine both sets so that I can use where and group clause?

Dheer
  • 773
  • 1
  • 6
  • 16

1 Answers1

3

What you need is an #OR method for active record, unfortunately that isn't implemented yet, but it's already merged in github and will be released with rails 5

Here's the pull request and dhh's comment about when its going to be released

In the mean time you either have the option to use arel, something like mentioned here

Or there's the silly bad way of plucking ids of both queries and then creating a third query with those ids.

Community
  • 1
  • 1
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89