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?

2
  • Add the definition of User.tagged_with
    – max
    May 9, 2015 at 6:47
  • 1
    this is method of gem act_as_taggable
    – Dheer
    May 9, 2015 at 7:00

1 Answer 1

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.

1
  • Yes, I need as #OR method for active record, Thanks for quick response its help me.
    – Dheer
    May 9, 2015 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.