Ruby on Rails: errors.add_to_base vs. errors.add
A missing genre would be a field error. A base error would be something like an exact duplicate of an existing record, where the problem wasn't tied to any specific field but rather to the record as a whole (or at lest to some combination of fields).
What is the Rails3 version of errors.add_to_base?
This should work in rails 3.1.3:
errors.add :base, "message"
Does add_to_base in rails adds a message to Activerecord::Base?
ActiveRecord::Base is the class that all ActiveRecord classes inherit from and it's quite confusing to think of it as the thing that the base object is derived from even though they share the same name. Base in the context of add_to_base means an instance of Foo < ActiveRecord::Base (for example)
It adds it to the base object rather than attaching any notion of errors directly to an attribute, this may be because an error may not specifically mention any attributes that the person may be changing or the error is associated with multiple attributes.
For Rails 3 - its errors.add(:base, msg)
Undefined method add_to_base
add_to_base
method was removed from rails 3. You should use errors[:base] << "error"
instead.
After upgrading to Rails 7, errors[:base] not working as expected
I can not explain why, but testing it myself i was able to reproduce this error, but using:
errors.add(:base, 'foo')
instead of:
errors[:base] << 'foo'
works and prevents the record from saving.
How to add active record validation errors inside custom setter?
Okay, i understood the core of the issue. It's not possible to do what i want to achieve, because all the validations errors cleared out as soon as validation process starts.
https://github.com/rails/rails/blob/75b985e4e8b3319a4640a8d566d2f3eedce7918e/activemodel/lib/active_model/validations.rb#L178.
Custom setter kicks in way too early :(
Related Topics
Error When Starting Rails Server: Warning: Insecure World Writable Dir /Usr in Path, Mode 040777
Ruby on Rails, Two Models in One Form
Capistrano & Bash: Ignore Command Exit Status
How to Force Ruby to Show a Full Stack Trace
How to Use Nokogiri to Parse an Xml File
Rails 3 Initializes Extremely Slow on Ruby 1.9.2
Post JSON Data to Simple Rails Application with Curl
Rubymine: Rails Server Launcher Wasn't Found in the Project
Spinach VS Cucumber for Bdd in Rails
How to Rescue Model Transaction and Show the User an Error
What Is an Elegant Way in Ruby to Tell If a Variable Is a Hash or an Array
Ruby Koan 151 Raising Exceptions
Listing the Names of Associated Models
How to Use Rails and Paperclip to Store Photos on Google Cloud Storage
Sending a Post Request with Net/Http
How to Switch to Older Versions of the Ruby/Rails Environment
Ruby/Rails Using || to Determine Value, Using an Empty String Instead of a Nil Value