How to make Active Record join return unique objects?

@dbjohn has the right idea, but I assume you want to avoid creating extra objects. Here’s a slight variant on his solution, letting the database do the uniq-ing for you:

date = "2013-01-01 00:00:00"
User.joins(:orders).where("orders.created_at >= ?", date).distinct

Note that you can rearrange the order of methods to fit whatever you think is most semantic, and ActiveRecord will write the same SQL for you.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)