How can I find(:all) on a parent model only for objects where a has_one
child is nil?
Models
class Parent < ActiveRecord::Base
has_one :child
end
class Child < ActiveRecord::Base
belongs_to :parent
end
So, I want to do:
object = Parent.find(:all) where the only rows returned match
object.child.nil?
I want this to happen on the database level because we will be dealing
with
thousands of rows. Is there an ActiveRecord way to handle this, or am I
going to have to break out to find_by_sql for this?
Thanks Shai. I’ve been dipping into with_scope the last couple of days
and
it’s really interesting… especially when it comes to nesting. I’m not
quite sure how I can utilize it in my situation though where I’m doing a
left join on two models with a has_one relationship. Every example of
with_scope I’ve seen as been acting within a single model. I’ll keep
playing
with it. In the meantime, find_by_sql is getting the job done.