If I have the following has_one setup:
class Account
has_one :user
How can I do something like @account.user.where(:visible => true)
Or more specifically, how do I call conditions on a has_one relationship in a similar way to a has_many? I’m currently using a scope on the user which seems silly?
def is_visible?
if self.visible
return self
else
return false
end
You’ll need to use a join to get the list of visible user accounts, but it’s not too hard once you grok the query. Try this:
You could also easily encapsulate it into a scope.