开发者

How do I re-write "Feedback.where('poster_id = 3 OR receiver_id = 3')" in Rails 3.1?

开发者 https://www.devze.com 2023-04-10 03:29 出处:网络
Is there a way to write it in a more \'Rails-friendly\' way? i.e. if I were searching for anyone of those attribute开发者_如何学编程s, I would just do Feedback.where(:poster_id => 3) and it would

Is there a way to write it in a more 'Rails-friendly' way?

i.e. if I were searching for anyone of those attribute开发者_如何学编程s, I would just do Feedback.where(:poster_id => 3) and it would be good.

But how do I achieve the OR in the Rails 3 friendly syntax (as opposed to the SQL friendly one).

Also, if it is better to use my original one over the desired one, why is it better?

Thanks.

Edit 1: Btw, if I do Feedback.where(:poster_id => 3, :receiver_id => 3) that returns the result for the AND operation, which is the exact opposite of what I want. So I feel so close, just not quite there.


You can do this by putting SQL fragments in the where() arguments. For more information you can look at the ActiveRecord querying guide.

Feedback.where("poster_id = ? OR receiver_id = ?", 1, 3)

You can do this without SQL fragments as described in this SO post:

t = Feedback.arel_table
Feedback.where(t[:poster_id].eq(1).or(t[:receiver_id].eq(2)))
0

精彩评论

暂无评论...
验证码 换一张
取 消