# Validation nightmare

**URL:** <https://www.ruby-forum.com/t/validation-nightmare/57129>\
**Category:** Rails\
**Created:** [April 7, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129 "2006-04-07T12:30:38Z")\
**Posts on this page:** 7\
**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:30pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/1 "2006-04-07T12:30:38Z")

</div>

Please help, I am really at a loss at how validation is supposed to work  
in rails.

Model contains

acts\_as\_tree

I want to force my NEW objects to have a parent  
I do NOT want existing object to have a parent

so I only want to have parent\_id on create, NOT on update.

I am trying this:

```
def validate_on_create
  validates_presence_of :parent_id, :message => "You must specify a 

```

parent for this keyword."  
end

and I get:

undefined method `validates\_presence\_of’ for #Keyword:0xb774aba0

The only time it works is if I remove validate\_on\_create, but then I can  
no longer update existing entries that do not have a parent!

this is driving me mad.

please help.

many thanks

Isabelle

---

<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:16pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/2 "2006-04-07T13:16:22Z")

</div>

Isabelle wrote:

> Please help, I am really at a loss at how validation is supposed to work  
> in rails.
> 
> Model contains
> 
> acts\_as\_tree
> 
> I want to force my NEW objects to have a parent  
> I do NOT want existing object to have a parent
> 
> so I only want to have parent\_id on create, NOT on update.
> 
> I am trying this:
> 
> ```
> def validate_on_create
> validates_presence_of :parent_id, :message => "You must specify a 
> 
> ```
> 
> parent for this keyword."  
> end
> 
> and I get:
> 
> undefined method `validates\_presence\_of’ for #Keyword:0xb774aba0
> 
> The only time it works is if I remove validate\_on\_create, but then I can  
> no longer update existing entries that do not have a parent!
> 
> this is driving me mad.
> 
> please help.
> 
> many thanks
> 
> Isabelle

Hi,

maybe try

validates\_presence\_of :parent\_id, :message =\> “You must specify a parent  
for this keyword.”, :on =\> :create

## ?

Agnieszka

---

<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:49pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/3 "2006-04-07T13:49:32Z")

</div>

> validates\_presence\_of :parent\_id, :message =\> “You must specify a parent  
> for this keyword.”, :on =\> :create

of course!

many thanks.

---

<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, 2:55pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/4 "2006-04-07T14:55:19Z")

</div>

Isabelle wrote:

> > validates\_presence\_of :parent\_id, :message =\> “You must specify a parent  
> > for this keyword.”, :on =\> :create
> 
> of course!
> 
> many thanks.

replied too early.

your solution does not work if I want to do several verifications on  
create.  
It also does not answer why I was getting the error.

So my problem remains.

This should work, but somehow it doesn’t:

def validate\_on\_create  
validates\_presence\_of :parent\_id, :message =\> “message.”

# more methods here

end

returns:  
undefined method `validates\_presence\_of’ for #Keyword:0xb774aba0

---

<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, 3:16pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/5 "2006-04-07T15:16:39Z")

</div>

Hi Isabelle,

Isabelle wrote:

Just to check assumptions, where does the code below live? The model,  
controller, or somewhere else?

Best regards,  
Bill

---

<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 27, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/6 "2006-04-27T18:33:03Z")

</div>

thanks Jeff, now I see how it works.

Isabelle

---

<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, 3:59pm UTC](https://www.ruby-forum.com/t/validation-nightmare/57129/7 "2006-04-07T15:59:49Z")

</div>

Isabelle wrote:

> Isabelle wrote:
> 
> > > validates\_presence\_of :parent\_id, :message =\> “You must specify a parent  
> > > for this keyword.”, :on =\> :create
> > 
> > of course!
> > 
> > many thanks.
> 
> replied too early.
> 
> your solution does not work if I want to do several verifications on  
> create.  
> It also does not answer why I was getting the error.
> 
> So my problem remains.
> 
> This should work, but somehow it doesn’t:
> 
> def validate\_on\_create  
> validates\_presence\_of :parent\_id, :message =\> “message.”
> 
> # more methods here
> 
> end
> 
> returns:  
> undefined method `validates\_presence\_of’ for #Keyword:0xb774aba0

validates\_presence\_of is designed to be used on its own, not in a  
method–it’s a class method rather than an instance method. If you want  
to make your own custom “validate\_on\_create”, then use Ruby to define  
the conditions yourself.

def validate\_on\_create  
if parent\_id.blank?  
errors.add(:parent\_id, “cannot be blank.”)  
end  
if other\_condition == true  
errors.add\_to\_base(“You can test as many conditions as you want in  
this method.”)  
end  
end

That’s how the custom validation functions work–you test the conditions  
and add to “errors” to flag a condition as invalid.

Alternatively, the way described above doesn’t actually keep you from  
supplying multiple conditions on create:

validates\_presence\_of :parent\_id, :name, :price, :message =\> “cannot  
be blank.”, :on =\> :create  
validates\_numericality\_of :price, :on =\> :create  
validates\_acceptance\_of :terms\_of\_service, :on =\> :create

…and so on.

Check the Agile Web D. With Rails book for more examples, it’s  
an invaluable resource. The Rails API documentation will have the  
specifics on how to use “errors.add” and “errors.add\_to\_base”.

Hope this helps,

Jeff C.man
