开发者

Rails find through association

开发者 https://www.devze.com 2022-12-30 19:16 出处:网络
class Customer < ActiveRecord::Base has_one:address, :foreign_key => \"customerid\" end class Address < ActiveRecord::Base
class Customer < ActiveRecord::Base
    has_one     :address, :foreign_key => "customerid"
end

class Address < ActiveRecord::Base
    belongs_to :customer, :foreign_key => "customerid"
end

How do I find records in customer that do not have customerid in address table?

in SQL i'd do

select * from customer a, address b where a.customerid <> b.开发者_开发问答customerid


This will return customers that doesn't have address:

Customer.find :all, 
   :conditions => 'id NOT IN (select distinct customerid from address)'

Hope I understood your question correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消