Forum: Ruby on Rails validation nightmare

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-07 12:30
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
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-04-07 13:16
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
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-07 13:49
> validates_presence_of :parent_id, :message => "You must specify a parent
> for this keyword.", :on => :create


of course!

many thanks.
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-07 14:55
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>
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-07 15:16
(Received via mailing list)
Hi Isabelle,

Isabelle wrote:

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

Best regards,
Bill
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-07 15:59
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 Development 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 Coleman
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 isabelle (Guest)
on 2006-04-27 18:33
thanks Jeff, now I see how it works.

Isabelle
This topic is locked and can not be replied to.