# Errors.add\_to\_base

**URL:** <https://www.ruby-forum.com/t/errors-add-to-base/93718>\
**Category:** Rails\
**Created:** [April 7, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/errors-add-to-base/93718 "2006-04-07T12:54:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isabelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5a626/32.png) [@Isabelle](https://www.ruby-forum.com/u/Isabelle)\
**Post date:** [April 7, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/errors-add-to-base/93718/1 "2006-04-07T12:54:43Z")

</div>

What are the limitations on using:

errors.add\_to\_base

to display errors in views?

I have tried for days to add errors from my object.rb and they never get  
displayed.

class Keyword \< ActiveRecord::Base  
validates\_presence\_of(:name, :message =\> “Name is required.”)  
validates\_uniqueness\_of(:name, :message =\> “This name is already in  
use. Please try another.”)

```
# counter_cache should be set to true, but it is broken!
acts_as_tree :order => "name", :counter_cache => false

def set_parent(parent_name)
  if (parent_name == '')

```

###### WHY IS THIS NOT WORKING ???

```
    errors.add_to_base("Name of parent cannot be empty")
  elsif (self.name == parent_name)
    errors.add_to_base("Parent invalid: a keyword cannot be a parent 

```

of itself!")  
else  
@parent = Keyword.find(:first, :conditions =\> [ “name = ?”,  
parent\_name ])  
if @parent  
self.parent\_id = @parent.id  
# this is necessary as long as the counter\_cache of  
acts\_as\_tree is broken  
@parent.update\_children\_count  
else  
errors.add\_to\_base("Parent invalid! No keyword found with  
name: " + parent\_name)  
end  
end  
end

The controller has:

@keyword.set\_parent(parent\_name)

The view has:

\<%= error\_messages\_for ‘keyword’ %\>

the messages from the validates\_ methods do get displayed. But why do  
the ones from set\_parent(parent\_name) never appear even if set\_parent  
gets executed and I know?

---

<div class="post-metadata">

**Author:** ![Isabelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5a626/32.png) [@Isabelle](https://www.ruby-forum.com/u/Isabelle)\
**Post date:** [April 7, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/errors-add-to-base/93718/2 "2006-04-07T13:01:50Z")

</div>

I would think that your problem lies with the logic and not  
‘add\_to\_base’.

Try putting some debug messages via logger (or anything else) in your  
code to see if that code block is ever getting executed. Please a  
debug statement before and after your ‘add\_to\_base’ call, and let me  
know if those ever even fire.

On 4/7/06, Isabelle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ###### WHY IS THIS NOT WORKING ???
> 
> acts\_as\_tree is broken
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://www.michaelgorsuch.org](http://www.michaelgorsuch.org)

---

<div class="post-metadata">

**Author:** ![Isabelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5a626/32.png) [@Isabelle](https://www.ruby-forum.com/u/Isabelle)\
**Post date:** [April 7, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/errors-add-to-base/93718/3 "2006-04-07T13:37:42Z")

</div>

Michael G. wrote:

> I would think that your problem lies with the logic and not  
> ‘add\_to\_base’.
> 
> Try putting some debug messages via logger (or anything else) in your  
> code to see if that code block is ever getting executed. Please a  
> debug statement before and after your ‘add\_to\_base’ call, and let me  
> know if those ever even fire.

the event gets fired. I use logger.debug, raise errors, they all are  
fine. The problem is that the errors object simply is not passed on.

I would very much appreciate it if somebody would post a model class  
that contains

errors.add\_to\_base

in methods other than validates\_ or validate\_ and tell me that it works!

many thanks for your help

Isabelle

PS: I have given up putting any validation in the model for  
parent/children objects. This means duplication of code for  
update/create.

the following is not elegant, but it works:

#keywords\_controller.rb

def create  
# keep entered parameters in memory so the user  
# does not have to re-enter everything if validation fails.  
@keyword = Keyword.new(params[:keyword])  
parent\_name = params[:parent][:name].strip

```
if (parent_name != '' )
  # reject auto-reference
  if parent_name == params[:keyword][:name]
    flash[:notice] = 'Parent invalid: a keyword cannot be a parent 

```

of itself!’  
render :action =\> ‘new’  
return false  
else  
@parent = Keyword.find(:first, :conditions =\> [ “name = ?”,  
parent\_name ])  
if @parent  
params[:keyword][:parent\_id] = @parent.id  
@keyword = Keyword.new(params[:keyword])  
else  
flash[:notice] = 'Parent invalid! No keyword found with name:  
’ + parent\_name  
render :action =\> ‘new’  
return false  
end # if @parent  
end # if parent\_name  
else  
# Force all new keywords to be attached to a parent  
flash[:notice] = ‘You must specify a parent for this keyword.’  
render :action =\> ‘new’  
return false  
end  
if @keyword.save  
# this is necessary as long as the counter\_cache of acts\_as\_tree  
is broken  
@parent.update\_children\_count  
redirect\_to :action =\> ‘show’, :id =\> @keyword  
return  
end  
end

I am sure there are better ways but I wasn’t able to find anything on  
google. Any hints greatly appreciated!
