Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource instance.valid? has no validate callbacks

#678

Taking an example from the docs:

class Person < ActiveResource::Base
   self.site = "http://www.localhost.com:3000/"
   protected
     def validate
       errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/
     end
end

As you can see valid? does not trigger any callbacks like ActiveRecord:

person = Person.new("first_name" => "Jim", "phone_number" => "I will not tell you.")
person.valid?   # => true
person.errors.add("phone_number", "has invalid format") unless :phone_number =~ /[0-9]*/
person.valid?   # => false

Reported by Hugh · July 23rd, 2008 @ 05:09 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Smilinguy
    Smilinguy

    I'm also having the same issue where my validate method is not called on valid?.

    In fact, my validate isn't even called on save! unless there have been attributes modified.

    In my case, these objects were created before I added new validation rules. If I try and save! them now, I would expect them to fail validation but they don't.

    July 27th, 2008 @ 06:58 AM

  2. Paul Horsfall
    Paul Horsfall

    I think that all validation in Active Resource is done by the remote service. There's "an old patch on Trac":

    http://dev.rubyonrails.org/ticke... to fix the documentation which I was thinking about reviving to clear things up. Should I?

    July 30th, 2008 @ 01:06 PM

  3. Tim Morton
    Tim Morton

    I would vote in favor of fixing the code to match the docs, not vice versa. Local validations are useful.

    See http://www.akitaonrails.com/2008... for a start.

    August 18th, 2008 @ 05:04 PM

  4. josh
    josh
    • State changed from new to wontfix

    ARes doesn't have validations or callbacks yet. I'm assuming this is coming when we switch it over to ActiveModel.

    November 22nd, 2008 @ 07:00 PM

  5. Thijs
    Thijs

    The documentation still says you can add a def validate to validate locally. Shouldn't this be removed untill it's actually working?

    December 2nd, 2008 @ 12:51 PM