# Active Record errors value - Why doesn't this work?

**URL:** <https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937>\
**Category:** Rails\
**Created:** [February 18, 2006, 1:59am UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937 "2006-02-18T01:59:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 18, 2006, 1:59am UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/1 "2006-02-18T01:59:22Z")

</div>

I’m creating an AR object in my controller and then attempting to add  
error messages to it:

if (master\_storeids.nil?)  
@item.errors.add\_to\_base ‘No store is selected’  
end

This works without error but when it comes time for AR to add the  
record, it apparently initializes the errors because the message that I  
added does not cause the AR add to fail as you would expect.

Even when there is a validation error in AR the message that I added  
does not show up in the list of errors on the resulting page.

So, am I doing something wrong? If not, how can I add my controller  
errors to the AR errors so that they are reported on the resulting page  
(and cause AR to not add the record)?

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 19, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/2 "2006-02-19T17:34:40Z")

</div>

Anyone? Any ideas at all?

This input form has fields that are not directly part of the record(s)  
being added. I need some way to detect errors in those fields and  
report to user when form is redisplayed with errors.

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 19, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/3 "2006-02-19T17:56:27Z")

</div>

What code are you using to display the errors in the view?

On 19 Feb 2006 16:33:08 -0000, Richard W. \<

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 20, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/4 "2006-02-20T19:37:39Z")

</div>

Richard W. wrote:

> record, it apparently initializes the errors because the message that I  
> added does not cause the AR add to fail as you would expect.
> 
> Even when there is a validation error in AR the message that I added  
> does not show up in the list of errors on the resulting page.
> 
> So, am I doing something wrong? If not, how can I add my controller  
> errors to the AR errors so that they are reported on the resulting page  
> (and cause AR to not add the record)?

valid? calls errors.clear, so you’d have to do:

if @item.valid? && master\_storeids  
[SAVE/ADD ITEM]  
elsif master\_storeids.nil?  
@item.errors.add\_to\_base ‘No store is selected’  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 20, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/5 "2006-02-20T19:00:46Z")

</div>

I want to use the “standard” error display built by the scaffold. You  
know the one where there is a message at the top of the form and the AR  
field in error is highlighted.

This works fine for AR field errors but I can’t seem to add errors to it  
(from outside the class by using the AR object’s add\_to\_base method.

Help anyone?

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 20, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/6 "2006-02-20T20:08:26Z")

</div>

Except with that approach you have to include this logic each time you  
are doing a save in the controller. I would suggest this is business  
logic tied to the model and therefore he should be using the validate  
method.

def validate  
if master\_storeids.nil?  
errors.add(“No store is selected”)  
end  
end

See page 267 in the Agile Rails book.

Michael

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [February 21, 2006, 12:31am UTC](https://www.ruby-forum.com/t/active-record-errors-value-why-doesnt-this-work/69937/7 "2006-02-21T00:31:17Z")

</div>

Unfortunately storeids is not a field of the masters table so I don’t  
think that will work.
