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')
Written by Diogo
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Activerecord
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#