1

我有一个Transaction模型。一个事务有一个seller_id列和一个buyer_id. 两者都充满了UserID。

所以:

class Transaction
  belongs_to :seller, :class_name => 'User'
  belongs_to :buyer, :class_name => 'User'
end

_

class User
  has_many :selling_transactions, :class_name => 'Transaction', :foreign_key => 'seller_id'
  has_many :buying_transactions, :class_name => 'Transaction', :foreign_key => 'buyer_id'
end

我想要做的是添加一个has_manyUser关联不完整的交易,无论User是卖方还是买方。

class User
  has_many :incomplete_transactions, :class_name => 'Transaction', :conditions => ???
end

_

我用纯 SQL 写出来,得到了我想要的结果。我的 SQL 中的联接是:

left outer join transactions t on ((t.seller_id = users.id and t.buyer_id is NULL) or (t.buyer_id = users.id and t.seller_id is NULL))

我如何将其join转化为has_many关联?

编辑:
我希望将不完整事务保留为 ActiveRecord::Relation (而不是数组),因此我可以执行类似 user.incomplete_transactions.limit(15) 的操作

谢谢。

4

4 回答 4

1

pdevisser 的类似答案:您可以使用与此问题上给出的答案类似的答案:https ://stackoverflow.com/a/307724/624590

这基本上会导致:

class User
  has_many :selling_transactions, :class_name => 'Transaction', :foreign_key => 'seller_id'
  has_many :buying_transactions, :class_name => 'Transaction', :foreign_key => 'buyer_id'

  has_many :incomplete_sales, :class_name => 'Transaction', :foreign_key => 'seller_id', :conditions => { :buyer_id => nil }
  has_many :incomplete_purchases, :class_name => 'Transaction', :foreign_key => 'buyer_id', :conditions => { :seller_id => nil }

  def incomplete_transactions
    incomplete_sales + incomplete_purchases
  end
end

编辑:好的,不完全确定如何按照您希望的方式进行设置(使用 has_many),但是这些方面的内容可能对您有用:

class User
  has_many :selling_transactions, :class_name => 'Transaction', :foreign_key => 'seller_id'
  has_many :buying_transactions, :class_name => 'Transaction', :foreign_key => 'buyer_id'

  def incomplete_transactions
    Transaction.where("(buyer_id = ? and seller_id = NULL) or (seller_id = ? and buyer_id = NULL)", id, id)
  end
end

where 语句将返回一个ActiveRecord::Association,因此您可以在调用它时使用 limit (或其他 activerecord 函数)跟随它。

于 2012-08-08T20:52:47.170 回答
0

您可以使用如下选项

:conditions = ["(t.seller_id = #{self.id} and t.buyer_id is NULL) or (t.buyer_id = #{self.id} and t.seller_id is NULL)"]
于 2012-08-09T17:00:16.220 回答
0

解决它!

这与@DRobinson 的解决方案非常相似,但使用 proc 和 has_many 定义而不是定义本地方法。

根据 3.1 发行说明,您现在可以在条件下使用 proc!

在 proc 内部,self 是关联所有者的对象,除非您急于加载关联,在这种情况下,self 是关联所在的类。

class User < ActiveRecord::Base
  has_many :incomplete_transactions , :class_name => 'Transaction', 
    :conditions => proc { incomplete_sales + incomplete_purchases }

  has_many :incomplete_sales, :class_name => 'Transaction', :foreign_key => 'seller_id', :conditions => { :buyer_id => nil }
  has_many :incomplete_purchases, :class_name => 'Transaction', :foreign_key => 'buyer_id', :conditions => { :seller_id => nil }

  has_many :selling_transactions, :class_name => 'Transaction', :foreign_key => 'seller_id'
  has_many :buying_transactions, :class_name => 'Transaction', :foreign_key => 'buyer_id'
end

class Transaction < ActiveRecord::Base
  belongs_to :seller, :class_name => 'User'
  belongs_to :buyer, :class_name => 'User'

# scope :incomplete_sales , :conditions => {:buyer_id => nil}
# scope :incomplete_purchases , :conditions => {:seller_id => nil}
end

看:

于 2012-08-09T16:44:23.160 回答
0

假设您使用的是 rails 3.2,我建议您创建命名范围

class Transaction
  belongs_to :seller, :class_name => 'User'
  belongs_to :buyer, :class_name => 'User'

  scope :incomplete_sales, :conditions => { :buyer_id => nil }
  scope :incomplete_purchases, :conditions => { :seller_id => nil }
end

然后你可以访问不完整的,

user.selling_transactions.incomplete_sales
user.buying_transactions.incomplete_purchases

编辑- 也更正了上面的关联

如果你想限制,你总是可以用数组做以下

user.selling_transactions.incomplete_sales[0,15]
于 2012-08-08T20:39:08.710 回答