Ruby on Rails: Errors.Add_To_Base VS. Errors.Add

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



Leave a reply



Submit