Rectangle 27 135

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 (available in ActiveRecord 5+, or in 4.2 via where-or backport):

first_name_relation.or(last_name_relation)

Is there an "OR" version of merge?

@minohimself Probably because that is the actual result of merging your two relations. Note that merge is an intersection, not union.

For future reference, #or method has been added to ActiveRecord::Relation on Jan 2015, and it will be part of Rails 5.0, which will ship in late 2015. It allows use of the OR operator to combine WHERE or HAVING clauses. You can checkout HEAD if you need it prior the official release. See Merge Pull Request #16052 @Arcolye @AndrewMarshall @Aldo-xoen-Giambelluca

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 135

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 (available in ActiveRecord 5+, or in 4.2 via where-or backport):

first_name_relation.or(last_name_relation)

Is there an "OR" version of merge?

@minohimself Probably because that is the actual result of merging your two relations. Note that merge is an intersection, not union.

For future reference, #or method has been added to ActiveRecord::Relation on Jan 2015, and it will be part of Rails 5.0, which will ship in late 2015. It allows use of the OR operator to combine WHERE or HAVING clauses. You can checkout HEAD if you need it prior the official release. See Merge Pull Request #16052 @Arcolye @AndrewMarshall @Aldo-xoen-Giambelluca

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 19

merge
OR
AND

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}%")))

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', 'Fnke')

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 19

merge
OR
AND

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}%")))

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', 'Fnke')

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 12

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('Fnke')
  )
)

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

Thanks for pointing this out, helped me out a lot. The other answers only work for a small subset of fields, but for upwards of ten thousand ids in the IN statement, the performance can be very bad.

@KeesBriggs that's not true. I've used this in all versions of Rails 4.

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 12

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('Fnke')
  )
)

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

Thanks for pointing this out, helped me out a lot. The other answers only work for a small subset of fields, but for upwards of ten thousand ids in the IN statement, the performance can be very bad.

@KeesBriggs that's not true. I've used this in all versions of Rails 4.

ruby on rails - Combine two ActiveRecord::Relation objects - Stack Ove...

ruby-on-rails rails-activerecord arel
Rectangle 27 34

If both repositories have same kind of files (like two Rails repositories for different projects), fetching the repository with git-fetch

git fetch git://repository.url/repo.git master:branch_name
git merge branch_name

Would let you have conflicts and manually solve those for example with git-mergetool. kdiff3 can be used solely with keyboard, so 5 conflict file takes when reading the code just few minutes.

Remember to finish the merge with git-commit

git commit

At this point, the remote repository has been merged to current repository and conflicts solved like you wanted.

How do you merge two Git repositories? - Stack Overflow

git merge repository git-subtree
Rectangle 27 34

If both repositories have same kind of files (like two Rails repositories for different projects), fetching the repository with git-fetch

git fetch git://repository.url/repo.git master:branch_name
git merge branch_name

Would let you have conflicts and manually solve those for example with git-mergetool. kdiff3 can be used solely with keyboard, so 5 conflict file takes when reading the code just few minutes.

Remember to finish the merge with git-commit

git commit

At this point, the remote repository has been merged to current repository and conflicts solved like you wanted.

How do you merge two Git repositories? - Stack Overflow

git merge repository git-subtree
Rectangle 27 34

If both repositories have same kind of files (like two Rails repositories for different projects), fetching the repository with git-fetch

git fetch git://repository.url/repo.git master:branch_name
git merge branch_name

Would let you have conflicts and manually solve those for example with git-mergetool. kdiff3 can be used solely with keyboard, so 5 conflict file takes when reading the code just few minutes.

Remember to finish the merge with git-commit

git commit

At this point, the remote repository has been merged to current repository and conflicts solved like you wanted.

How do you merge two Git repositories? - Stack Overflow

git merge repository git-subtree
Rectangle 27 3

The best result for this that I've come across is to grab and merge the ids of the two queries and then search for them like this:

It's four lines that seems like it should be doable in one but it works.

There is no need for the uniq, because the database won't return duplicate rows. But i'm giving this an upvote because this is what i was going to suggest

ids
apple_companies = Company.where("name like ?","%apple%").ids banana_companies = Company.where("name like ?","%banana%").ids

ruby - Rails combine multiple activerecord relations - Stack Overflow

ruby ruby-on-rails-4 rails-activerecord activerecord-relation
Rectangle 27 3

The best result for this that I've come across is to grab and merge the ids of the two queries and then search for them like this:

It's four lines that seems like it should be doable in one but it works.

There is no need for the uniq, because the database won't return duplicate rows. But i'm giving this an upvote because this is what i was going to suggest

ids
apple_companies = Company.where("name like ?","%apple%").ids banana_companies = Company.where("name like ?","%banana%").ids

ruby - Rails combine multiple activerecord relations - Stack Overflow

ruby ruby-on-rails-4 rails-activerecord activerecord-relation
Rectangle 27 2

ActiveRecord::Relation.where accepts an ARel predicate, so in this case you can just pass your final predicate directly to Address.where.

class Address < ActiveRecord::Base
  scope :anywhere, -> search {
    addr_arel = Address.arel_table
    attrs = [:line1, :line2, :city, :state, :zip]

    where attrs
      .map {|attr| addr_arel[attr].matches("%#{search}%")}
      .inject(:or)
  }
end

ruby on rails - how to merge an arel object into an ActiveRecord::Rela...

ruby-on-rails ruby-on-rails-3 activerecord arel activerecord-relation
Rectangle 27 36

You should use the merge method

class Category < ActiveRecord::Base
  has_many :products
  scope :with_purchaseable_products, joins(:products).merge(Product.purchaseable).group(:id).having('count(products.id) > 0')
end

Ruby / Rails - Can I use a joined table's scope(or class method) as pa...

ruby-on-rails ruby-on-rails-3 activerecord scope arel
Rectangle 27 17

The & method doesn't look like it works anymore (too bad, I found the syntax was neat). You can replace it with ActiveRecord::Relation#merge:

class User < ActiveRecord::Base

  scope :published, lambda {
    joins(:posts).group("users.id").merge(Post.published)
  }
end

And it looks like it won't be back, trying it in rails 3.0.10 gives a deprecation warning:

DEPRECATION WARNING: Using & to merge relations has been deprecated and will be removed in Rails 3.1. Please use the relation's merge method, instead.

@charlysisto this file hasn't changed between v3.1.0 and master, are you sure it's the correct one?

Why using merge method with scopes isn't working anymore on Rails 3.1?...

ruby-on-rails-3 ruby-on-rails-3.1 scopes
Rectangle 27 9

You can use merge method in order to merge scopes from different models. For more details search for merge in this railscast

merge

How do you scope ActiveRecord associations in Rails 3? - Stack Overflo...

ruby-on-rails activerecord ruby-on-rails-3 named-scope arel
Rectangle 27 61

Depending on your needs and programming style, you can also use a combination of the new method of the class and merge. For lack of a better simple example, suppose you have a task scheduled for a certain date and you want to duplicate it to another date. The actual attributes of the task aren't important, so:

will create a new task with :id => nil, :scheduled_on => some_new_date, and all other attributes the same as the original task. Using Task.new, you will have to explicitly call save, so if you want it saved automatically, change Task.new to Task.create.

WARNING: Can't mass-assign protected attributes: id, due_date, created_at, updated_at

When I do this, I get an unknown attribute error with one column because of a column that is there due to a has_many relationship. Is there any way around this?

@RubenMartineJr. I know this is an old post, but yeah you can get around this by using '.except' on the attributes hash: new_task = Task.new(old_task.attributes.except(:attribute_you_dont_want, :another_aydw).merge({:scheduled_on => some_new_date}))

@PhillipKoebbe thank you - but what if I want the id to not be null? I want rails to automatically assign a new id when i create the duplicate - is this possible?

ruby on rails - What is the easiest way to duplicate an activerecord r...

ruby-on-rails ruby rails-activerecord
Rectangle 27 1

merge
Device.joins(:user).merge(-> { User.joins(:order)... })

Sorry, right not I cannot give any explanations because I don't know much about merge method. I just read the docs and saw Proc option.

ruby on rails - "Missing FROM-clause entry" when using nested merges w...

ruby-on-rails ruby postgresql activerecord
Rectangle 27 3

default_scope where('last_request >= :time OR (last_request < :time OR last_request IS NULL)', time: 1.day.ago).order('last_request DESC')

UPD That is not ActiveRecord or RailsAdmin issue, more info

Sorry, but what's the difference between doing this and just a default_scope with the order clause?

ruby - Rails merge 2 query results - Stack Overflow

ruby-on-rails ruby activerecord