Forum: Ruby on Rails @model.errors.empty? => true; @model.valid? => false

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.
James H. (Guest)
on 2006-06-01 21:48
(Received via mailing list)
Hi,

So, how can the situation described in the subject come to be?

In other words, what could be invalidating the model, yet not
generating an error?

thanks,
jh

ps.

here's the breakpoint session paste:

irb(#<#<Class:0xb72eec00>:0xb72eeb10>):003:0> @project.errors.empty?
=> true
irb(#<#<Class:0xb72eec00>:0xb72eeb10>):004:0> @project.valid?
=> false
irb(#<#<Class:0xb72eec00>:0xb72eeb10>):005:0>

--
James H.
Web application developer
Vancouver, BC

"Developing a coherent political analysis is in many respects
contingent upon an ability to connect one context to another, a
process not dissimilar to playing the kid's game of dot-to-dot."
 - Ward Churchill, from '"A Government of Laws"?'
Stephen B. (Guest)
on 2006-06-01 22:32
I think @project.valid? just runs the validation methods - the errors
aren't added to the object until a save is carried out.

Steve

James H. wrote:
> Hi,
>
> So, how can the situation described in the subject come to be?
>
> In other words, what could be invalidating the model, yet not
> generating an error?
>
> thanks,
> jh
>
> ps.
>
> here's the breakpoint session paste:
>
> irb(#<#<Class:0xb72eec00>:0xb72eeb10>):003:0> @project.errors.empty?
> => true
> irb(#<#<Class:0xb72eec00>:0xb72eeb10>):004:0> @project.valid?
> => false
> irb(#<#<Class:0xb72eec00>:0xb72eeb10>):005:0>
>
> --
> James H.
> Web application developer
> Vancouver, BC
>
> "Developing a coherent political analysis is in many respects
> contingent upon an ability to connect one context to another, a
> process not dissimilar to playing the kid's game of dot-to-dot."
>  - Ward Churchill, from '"A Government of Laws"?'
Jodi S. (Guest)
on 2006-06-01 22:42
(Received via mailing list)
no, that's not true Steve. the valid? method runs validation and
populates errors.

James, I think I'd comment out your validations (on at a time) - see
what changes. standard prod and voila debug. I think I'd also take a
look at errors.full_messages <-- might it have values when
errors.empty? not sure...

Another thought - have you tried the same using script/console - this
loads your models as per the environment. Will probably help with
debugging. I think irb will require more help from you to get all
setup predictably.

Jodi
James H. (Guest)
on 2006-06-01 22:42
(Received via mailing list)
Ok, after some more poking around I see that valid? has the side
effect of populating the errors object if validations fail, which
explains my irb session below.

What I don't get is that AWDWR and all the API docs give the
impression that save() and update_attributes() perform validations
when they are called. From my experience and from a cursory glance at
the code for update_attributes(), this would seem to not be the case.

Which leads me to the conclusion that I would have to write my
controller update like this:

    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
      flash[:notice] = 'Project was successfully updated.'
      redirect_to :action => 'show', :id => @project
    else
      @project.valid?
      edit
      render :action => 'edit'
    end

in order to get error_messages_for to work as expected in the view;
eg. I have to call the method valid?(), when I already know the object
is invalid. That can't be right.

Can someone explain what I'm missing here?

thanks.
jh

On 6/1/06, James H. <removed_email_address@domain.invalid> wrote:
> ps.
> James H.
> Web application developer
> Vancouver, BC
>
> "Developing a coherent political analysis is in many respects
> contingent upon an ability to connect one context to another, a
> process not dissimilar to playing the kid's game of dot-to-dot."
>  - Ward Churchill, from '"A Government of Laws"?'
>


--
James H.
Web application developer
Vancouver, BC

"Developing a coherent political analysis is in many respects
contingent upon an ability to connect one context to another, a
process not dissimilar to playing the kid's game of dot-to-dot."
 - Ward Churchill, from '"A Government of Laws"?'
James H. (Guest)
on 2006-06-01 23:07
(Received via mailing list)
On 6/1/06, Jodi S. <removed_email_address@domain.invalid> wrote:
> no, that's not true Steve. the valid? method runs validation and
> populates errors.

Yes, and according to AWDWR (p.267) valid?()  "invokes the same two
validation methods that would be invoked if save() had been called".

That's not what I'm seeing. update_attributes() (which in turn calls
save)[1], is returning false (i.e. the record is not valid), but is
*not* populating @project.errors.

> Another thought - have you tried the same using script/console - this
> loads your models as per the environment. Will probably help with

That irb session I pasted is at a breakpoint in the controller's
update method, so the environment is set up correctly.


[1]
      def update_attributes(attributes)
        self.attributes = attributes
        save
      end

> >>
> >> here's the breakpoint session paste:
> >> Vancouver, BC
> > Rails mailing list
> > removed_email_address@domain.invalid
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
James H.
Web application developer
Vancouver, BC

"Developing a coherent political analysis is in many respects
contingent upon an ability to connect one context to another, a
process not dissimilar to playing the kid's game of dot-to-dot."
 - Ward Churchill, from '"A Government of Laws"?'
Stephen B. (Guest)
on 2006-06-01 23:10
> no, that's not true Steve. the valid? method runs validation and
> populates errors.
Right you are - my bad.

Steve


Jodi S. wrote:
> no, that's not true Steve. the valid? method runs validation and
> populates errors.
>
> James, I think I'd comment out your validations (on at a time) - see
> what changes. standard prod and voila debug. I think I'd also take a
> look at errors.full_messages <-- might it have values when
> errors.empty? not sure...
>
> Another thought - have you tried the same using script/console - this
> loads your models as per the environment. Will probably help with
> debugging. I think irb will require more help from you to get all
> setup predictably.
>
> Jodi
James H. (Guest)
on 2006-06-01 23:13
(Received via mailing list)
On 6/1/06, Stephen B. <removed_email_address@domain.invalid> wrote:
> I think @project.valid? just runs the validation methods - the errors
> aren't added to the object until a save is carried out.

My experience shows the opposite: valid? is adding the errors, save is
not. But the docs say that save *does* add to the errors.

I guess my question comes down to: What am I doing/not doing that is
preventing update_attributes/save from populating the errors object?

I should point out that I am not overriding validate and validate_on_*
methods. Should that matter? It wouldn't seem to, as valid? is running
my validations just fine. (fyi, the validations are just a big list of
validates_presence_of calls).

> >
> > --
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
James H.
Web application developer
Vancouver, BC

"Developing a coherent political analysis is in many respects
contingent upon an ability to connect one context to another, a
process not dissimilar to playing the kid's game of dot-to-dot."
 - Ward Churchill, from '"A Government of Laws"?'
Stephen B. (Guest)
on 2006-06-01 23:25
Yeah - i was completely wrong about valid? - it does add errors - but so
does save and update_attributes.

Here's some of my code i have working:

  def update
    @page = Page.find(params[:id])
    if @page.update_attributes(params[:page])
      flash[:notice] = 'Page was successfully updated.'
      redirect_to :action => 'edit', :id => @page
    else
      render :action => 'edit'
    end
  end

The only differences i can see is that you call the 'edit' controller
method and @project.valid?

Steve


James H. wrote:
> On 6/1/06, Stephen B. <removed_email_address@domain.invalid> wrote:
>> I think @project.valid? just runs the validation methods - the errors
>> aren't added to the object until a save is carried out.
>
> My experience shows the opposite: valid? is adding the errors, save is
> not. But the docs say that save *does* add to the errors.
>
> I guess my question comes down to: What am I doing/not doing that is
> preventing update_attributes/save from populating the errors object?
>
> I should point out that I am not overriding validate and validate_on_*
> methods. Should that matter? It wouldn't seem to, as valid? is running
> my validations just fine. (fyi, the validations are just a big list of
> validates_presence_of calls).
>
>> >
>> > --
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
> James H.
> Web application developer
> Vancouver, BC
>
> "Developing a coherent political analysis is in many respects
> contingent upon an ability to connect one context to another, a
> process not dissimilar to playing the kid's game of dot-to-dot."
>  - Ward Churchill, from '"A Government of Laws"?'
James H. (Guest)
on 2006-06-01 23:35
(Received via mailing list)
Figured it out. See below.

On 6/1/06, James H. <removed_email_address@domain.invalid> wrote:
> On 6/1/06, Stephen B. <removed_email_address@domain.invalid> wrote:
> > I think @project.valid? just runs the validation methods - the errors
> > aren't added to the object until a save is carried out.
>
> My experience shows the opposite: valid? is adding the errors, save is
> not.

Wrong, wrong, wrong. save() is performing as advertised, but a certain
braindead programmer is undoing all it's hard work by reinitializing
the model object before rendering the view. Ack.

> I guess my question comes down to: What am I doing/not doing that is
> preventing update_attributes/save from populating the errors object?

Nothing!

Oh well, at least I have a pretty thorough understanding of how
validations work now. Thanks for chiming in on this.
jh
Jodi S. (Guest)
on 2006-06-01 23:38
(Received via mailing list)
Hey James, perhaps using the helpers within your over-ridden
validates is the culprit (messing up an super.init or something).

(I've never used helpers within a def validate, so I can't comment
whether this may or may not work)

How about dropping the validate over-ride for a moment, and just use
the helpers. That'll tell us a new symptom.

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