Last Updated: February 25, 2016
· diogoandre

Mixing query conditions in AR

This might seem obivious, but it took me some time a while ago to figure it out.

You can't mix condition types inside a single where method in Rails:

Post.where(:author_id => my_id, 'pub_date = created_at')

in the case above, mixing a hash condition with a pure text condition would throw an error. A pure text condition is handy if need to compare values inside the row, in the case above I'm checking if the date published matches the date when the post was created.

In this case, we can just use multiple where methods:

Post.where(:author_id => my_id).where('pub_date = created_at')
Say Thanks