Searching associations in DataMapper 0.3
One of the first things you discover when you start playing with DataMapper is how to specify conditions when searching a database table. This will get you all the chocolate biscuits:
Biscuit.all(:chocolate => true)
After I'd read some of the docs I hoped I might be able to use a similar approach to search my associations. Given that a packet has_many
biscuits I hoped I'd be able to do something like this:
packet.biscuits(:broken => true)
I was wrong, and you'd probably only expect that to work if (like me) you hadn't done much with DataMapper yet. Still, you might.
In the current version (0.3.0) you do it like this:
Biscuit.all(:packet_id => packet.key, :broken => true)
That's fine. I dare say associations will grow the ability to be searched in due course, but in the meantime, maybe that'll help clarify it for somebody...
If you're interested in DataMapper (and merb?) then you might want to check out the open source book on the subject: Life On The Edge With Merb, DataMapper & RSpec.