ActiveRecord conditions with association from hash

I’m sure you all know how to use the :conditions attribute when using ActiveRecord:

User.find(:all, :conditions=>['active = ?', true])

And you may even use associations this way:

User.find(:all, :include=>[:photos], :conditions=>['photos.removed = ? and users.active = ?', false, true])

But did you know that you can do this easier through hashes?

User.find(:all, :conditions=>{:active=>true})
User.find(:all, :include=>[:photos], :conditions=>{'photos.removed'=>false, 'users.active'=>true})

Nothing special there, but I thought it was pretty cool. One thing you have to remember when using associations, is to include that model.

 

Tags: , , , ,

Leave a Reply