范围,我有两个模式:销售和支付的Rails 3:可选HAS_ONE协会
class Sale < ActiveRecord::Base
has_one :payment
end
class SaleCancelation < ActiveRecord::Base
belongs_to :payment
end
我要创建两个范围,“付款”和“不付款”。
“with_payment”作品easyly:
class Sale < ActiveRecord::Base
scope :with_payment, joins(:payment)
end
但我怎么可以创建一个发现每销售,做不具有关联的付款示波器?
(不能回答自己的问题,8小时,这样的解决方案放在这里)。 刚刚发现的解决方案,使用MetaWhere宝石(详细explenation在这里:http://metautonomo.us/2010/11/02/metawhere-is-about-to-get-func-y/ 'scope:without_payment ,加入(:payment.outer).where(:payment => {:id => nil})' (请注意**:payment.outer **,它创建一个外连接而不是内连接, ) – Majiy