views:

32

answers:

1

Hi All,

I have two tables, photos and tags, with a standard photos_tags link table.

My models show Photos HABTM Tags, and I can update both fine with the links updating too.

My question is: say I now want to find all Photos tagged with both "Sunset" and "Ocean" - how do I go about that?

I've tried doing an inner join as per Nate's Bakery post and I can now find all photos tagged with either OR both, but I want just those with both tags (e.g. an "AND" find, if that's the correct phrase). Currently adding more tags gives me more results - I want less results as more tags are added!

Alternatively, if there isn't a nice Cakey way, how would you do it? Multiple searches and then compare the arrays? Seems kinda wasteful though...

TIA.

A: 

You can bind condition arrays like this

'conditions' => array('AND' => array('field' => 'value', 'field' => 'value'))
zefciu
How would this work on HABTM? I'm not looking at two fields as such, but a pair of relationships.
I've had trouble citing two identical keys in a conditions array. I've had to wrap each in its own array to fix it. Like this...'conditions' => array( 'and' => array( array('field'=>'value'), array('field'=>'value2') )
Dan Berlyoung