views:

25

answers:

1

Hi,

Is there a way to specify and OR condition?

For example:

Message.find(1, :condition => {"profile_id = ? OR sender_id = ?", 2, 2})

Ultimately, I want to check whether a user has sent or received a message, but if I do them separate I an exception is thrown when the record is not found for one of the cases.

A: 

You're very close! Change the {}s to []s and

Message.find(:first, :condition => ["profile_id = ? OR sender_id = ?", profile_id, sender_id])

should do it for you.

(By the way, don't forget that it's also always possible to just use find_by_sql in case there's not a 'clever enough' helper current available...)

Joe