Reputation: 3938
I have a user that has_many associations. Associations has a provider field. I want to check if a user has a provider. If provider were a field on the user table I would just do provider.blank?
How do I do the same check when reaching through a has_many association?
EDIT:
class User < ActiveRecord::Base
has_many :authentications
end
class Authentications < ActiveRecord::Base
belongs_to :user
end
Authentications table has the following fields
:provider
:user_id
:uid
:id
Upvotes: 1
Views: 29
Reputation: 11588
You can check if any of the user's Authentications contain providers using the following:
user.authentications.any? {|a| a.provider }
any?
iterates over the Array and returns true
if the block returns true when passed each element of the Array. When the Array is empty (i.e., no Authentications) it returns false
.
Upvotes: 2