ruby Rails + ActiveRecord:我如何写一个条件“一个或两个都不为真”?

relj7zay  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(72)

在我的模型Listing中,除了其他列之外,我还有这两个列:

- seller_currency
- bidder_currency

listings.seller_currencylistings.bidder_currency的值可以是1(USD)或0(EUR)。
我试图得到所有的上市,其中seller_currencybidder_currency0(欧元)或至少有一个是0(欧元)。
还有比这更好的写条件的方法吗?

Listing.where('(seller_currency=0) OR (bidder_currency=0) OR (seller_currency=0 AND bidder_currency=0)')
wgeznvg7

wgeznvg71#

根据你的例子,至少满足一个条件就足够了。

Listing.where(seller_currency: 0).or(Listing.where(bidder_currency: 0))
nbewdwxp

nbewdwxp2#

你只需要一个或条件,它涵盖了它们都为0的情况。这种方式更可取,因为它是一个更快的查询,因为它同时计算两个列。此外,如果你想使用变量,问号会停止sql注入。
Listing.where("(seller_currency=?) OR (bidder_currency=?)",0,0)

相关问题