Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ActiveRecord automatically validates has_many association

Following models are given:

class Question < ActiveRecord::Base
  has_many  :answers
end

class Answers < ActiveRecord::Base
  belongs_to: question
  validates :comment, presence: true
end

When calling

question = Question.new
question.answers.build
question.valid?

valid? returns false because the associated answer is not valid. When writing

has_many :answers, validate: false

in Question valid?returns true.

Is it a bug or is it desired when using has_many the associated models are validated automatically? The Rails Guides explicitly explain the use of validate_associated with a has_many relationship: http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated

like image 566
dan Avatar asked Mar 28 '13 11:03

dan


People also ask

What is the difference between validate and validates in Rails?

validates is used for normal validations presence , length , and the like. validate is used for custom validation methods validate_name_starts_with_a , or whatever crazy method you come up with. These methods are clearly useful and help keep data clean. That test fails.

How does validation work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is Active Record association in Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship. Whether it; belongs_to, has_many, has_one, has_one:through, has_and_belongs_to_many.

What is Active Record in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

It's most definitely not a bug.

  • You've created a Question
  • You've told Rails to attach a new instance of Answer and relate it to this new Question
  • You're then asking "Rails, is this Question/Answer model and association I've created ready to be saved to the database?"

As you've found, Rails will say "No" in your case.

I have never used and do not care about validates_associated. I can however point you to documentation explaining why you're seeing the behavior you are.

  • Active Record Autosave Association

Though the documentation at the above source file is worth reading in it's entirety, I'll pull out this bit for you

Note that :autosave => false is not same as not declaring :autosave. When the :autosave option is not present new associations are saved.

  • You have not specified :autosave => SOMETHING on your :answers association
  • Because of this, Rails by default is going to try and save your newly build/associated Answer on your new Question
  • The save will fail because the Answer is invalid
like image 186
deefour Avatar answered Sep 26 '22 02:09

deefour