How to Validate the Presence of a Belongs to Association with Rails

Rails - Validate Presence Of Association?

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: Nested models and parent validation. In this topic you can find solution.

How do I validate an association value is present in Rails?

Please try using custom validation

class User < ApplicationRecord
validate :company_aum_id_present, if: :provider?

private def company_aum_id_present
self.errors[:aum_id] << 'Assets Under Management is required.' if company && company.aum_id.blank?
end
end

Also When usin custom validator you dont need to include ActiveModel::Validations as it is already included by ApplicationRecord

Rails: Validating existence of an association

http://blog.hasmanythrough.com/2007/7/14/validate-your-existence

class Post < ActiveRecord::Base
belongs_to :category
validates_presence_of :category
end

-OR-

class Post < ActiveRecord::Base
belongs_to :category
validates :category, presence: true
end

Rails versions prior to 3.2:

class Post < ActiveRecord::Base
belongs_to :category
validates_existence_of :category
end

Rails 6: How to conditionally validate an association for presence?

Starting from Rails 5, belongs_to adds presence validation by default.
You can disable this behavior in application.rb with:

config.active_record.belongs_to_required_by_default = false

Or if you want it to be optional only in this belongs_to you can pass optional option to belongs_to:

belongs_to :offer, optional: true

Check for existing association 'belongs_to' in Rails 4

You can use validates_associated to validate the associated object as follows:

class Order < ActiveRecord::Base
validates :user, :order_status, :delivery_type, presence: true
belongs_to :user

validates_associated :user
end

Note that you should not remove the presence validation even if you add validates_associated :user. Please see the following note from the documentation:

NOTE: This validation will not fail if the association hasn’t been
assigned. If you want to ensure that the association is both present
and guaranteed to be valid, you also need to use
validates_presence_of.

Rails 3: validate presence of at least one has many through association item

Side note before the answer, based on the structure of your models I would use has_and_belongs_to_many instead of using this explicit linking model since it appears the linking model doesn't add anything of value.

Either way though, the answer is the same, which is to use a custom validation. Depending on whether you go with things the way they are or simplify to a has_and_belongs_to many you'll want to validate slightly differently.

validate :has_project_disciplines

def has_project_disciplines
errors.add(:base, 'must add at least one discipline') if self.project_disciplinizations.blank?
end

or with has_and_belongs_to_many

validate :has_project_disciplines

def has_project_disciplines
errors.add(:base, 'must add at least one discipline') if self.project_disciplines.blank?
end


Related Topics



Leave a reply



Submit