I have this relationship : a user can have zero or one dog, but a dog has to belong to someone.
# dog.rb
class Dog < ActiveRecord::Base
belongs_to :user
end
# user.rb
class User < ActiveRecord::Base
has_one :dog
end
I want to define the following scopes :
User.with_a_dog
User.without_a_dog
I can do this for the first case, because the joins are INNER JOIN by default in rails :
scope :with_a_dog, :joins(:dog)
1/ Is this solution for the first scope good enough?
2/ What would you do for the second one?
3/ (Somewhat related) Is there a better way of doing this? :
# user.rb
def has_a_dog?
!self.dog.nil?
end
Thanks for your help!