如何使LIKE与Datamapper之间的OR条件?

时间:2012-08-09 16:58:35

标签: ruby sinatra datamapper

我有3个相关模型:

class Transaction
  include DataMapper::Resource    
  property :id, Serial     
  property :volume, Float
  property :deal_date, Date  
  belongs_to :buyer
  belongs_to :seller
end

class Seller
  include DataMapper::Resource    
  property :id,         Serial
  property :name,   String      
  has n, :transactions
end

class Buyer
  include DataMapper::Resource    
  property :id, Serial
  property :name,   String, :length => 255, :index => true, :unique => true
  has n, :transactions
end

我想查询有些条件的交易:

x < volume < y
and
a < deal_date < b
and
( buyer.name like key_word OR seller.name like key_word )

如何在两个LIKE和Datamapper之间制作OR条件?

2 个答案:

答案 0 :(得分:3)

只需查询Transaction,但查询路径指向buyer.nameseller.name

Transaction.all('buyer.name.like' => keyword) | Transaction.all('seller.name.like' => keyword)

答案 1 :(得分:0)

也许会这样:

key_word = '%blabla%'
trs = Transaction.all condition: [
        '   buyer_id IN (SELECT id FROM buyer WHERE name LIKE ?) 
         OR seller_id IN (SELECT id FROM seller WHERE name LIKE ?)', 
      key_word, key_word]