3

我有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 )

如何使用 Datamapper 在两个 LIKE 之间创建 OR 条件?

4

2 回答 2

3

简单地查询Transaction,但查询路径到buyer.nameseller.name

Transaction.all('buyer.name.like' => 关键字) | Transaction.all('seller.name.like' => 关键字)
于 2012-08-10T07:00:01.047 回答
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]
于 2014-09-23T22:28:15.510 回答