大致是这样: 报价 (Quote),有商务 (business) 负责做它,但由销售 (sale) 发给客户,所以模型如下:
class Quote < ActiveRecord::Base
belongs_to :sale, :class_name => 'User', :foreign_key => 'sale_user_id'
belongs_to :business, :class_name => 'User', :foreign_key => 'business_user_id'
end
class User < ActiveRecord::Base
has_many :quotes, :foreign_key => 'sale_user_id'
has_many :deal_quotes, :class_name => 'Quote', :foreign_key => 'business_user_id'
end
单独查询都没问题:
>> Quote.where("users.id = 14").includes("sale").size #销售14名下的报价
94
>> Quote.where("users.id = 11").includes("business").size #由商务11做的报价
105
现在我想把它们AND
起来,但就不对了啊,两个都是users.id
,结果就是 0……
有没有什么 alias 之类的东东来解决这个问题啊?