Search code examples
ruby-on-railsrubyvalidationbelongs-to

ROR : what does validate option do in belongs_to?


I'm new to ROR. Can anyone tell me what does the validate option do in belongs_to with an example?

class Product < ActiveRecord::Base
  belongs_to :category, validate => true
end

Solution

  • In rare cases Rails allows to create an parent object for the dependent, for example: product.create_category! (docs). About validate option docs:

    :validate If false, don’t validate the associated objects when saving the parent object. false by default.

    That means that when you save the product, by default the category is not validated. In your case the category will be validated.