Rails - Validate Presence Of Association?

Ruby on-RailsRuby on-Rails-3ValidationActiverecord

Ruby on-Rails Problem Overview


I have a model A that has a "has_many" association to another model B. I have a business requirement that an insert into A requires at least 1 associated record to B. Is there a method I can call to make sure this is true, or do I need to write a custom validation?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can use validates_presence_of http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of

class A < ActiveRecord::Base
  has_many :bs
  validates_presence_of :bs
end

or just validates http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates

class A < ActiveRecord::Base
  has_many :bs
  validates :bs, :presence => true
end

But there is a bug with it if you will use accepts_nested_attributes_for with :allow_destroy => true: https://stackoverflow.com/questions/5144527/nested-models-and-parent-validation. In this topic you can find solution.

Solution 2 - Ruby on-Rails

-------- Rails 4 ------------

Simple validates presence worked for me

class Profile < ActiveRecord::Base
  belongs_to :user

  validates :user, presence: true
end

class User < ActiveRecord::Base
  has_one :profile
end

This way, Profile.create will now fail. I have to use user.create_profile or associate a user before saving a profile.

Solution 3 - Ruby on-Rails

You can validate associations with validates_existence_of (which is a plugin):

Example snippet from this blog entry:

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, :polymorphic => true
  validates_existence_of :tag, :taggable

  belongs_to :user
  validates_existence_of :user, :allow_nil => true
end

Alternatively, you can use validates_associated. As Faisal notes in the comments below the answer, validates_associated checks if the associated object is valid by running the associated class validations. It does not check for the presence. It's also important to note that a nil association is considered valid.

Solution 4 - Ruby on-Rails

If you want to ensure that the association is both present and guaranteed to be valid, you also need to use

class Transaction < ActiveRecord::Base
  belongs_to :bank

  validates_associated :bank
  validates :bank, presence: true
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskazView Question on Stackoverflow
Solution 1 - Ruby on-Railsfl00rView Answer on Stackoverflow
Solution 2 - Ruby on-RailshexinpeterView Answer on Stackoverflow
Solution 3 - Ruby on-RailsSpyrosView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSukeerthi AdigaView Answer on Stackoverflow