You can simply add a unique index to the database table of the SocialMedia model. You can also add confirmation:
validates_uniqueness_of :facebook_id
And in your User model, you must check for the existence and existence of:
validate_presence_of :social_media_id
validate :social_profile_exists
protected
def social_profile_exists
errors.add(:social_media_id, "does not exist") unless SocialProfile.exists?(social_media_id)
end
Perhaps this has already worked:
validates_associated :social_media
must make sure that the corresponding entry is valid.
source
share