James Hughes
2006-Jun-01 17:46 UTC
[Rails] @model.errors.empty? => true; @model.valid? => false
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 Hughes 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 Bartholomew
2006-Jun-01 18:33 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
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 Hughes 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 Hughes > 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"?''-- Posted via http://www.ruby-forum.com/.
Jodi Showers
2006-Jun-01 18:40 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
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 On 1-Jun-06, at 2:32 PM, Stephen Bartholomew wrote:> 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 Hughes 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 Hughes >> 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"?'' > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
James Hughes
2006-Jun-01 18:41 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
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 Hughes <hughes.james@gmail.com> 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 Hughes > 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 Hughes 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 Hughes
2006-Jun-01 19:06 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
On 6/1/06, Jodi Showers <jodi@nnovation.ca> 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 withThat 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> Jodi > On 1-Jun-06, at 2:32 PM, Stephen Bartholomew wrote: > > > 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 Hughes 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 Hughes > >> 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"?'' > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- James Hughes 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 Bartholomew
2006-Jun-01 19:10 UTC
[Rails] Re: Re: @model.errors.empty? => true; @model.valid? => false
> no, that''s not true Steve. the valid? method runs validation and > populates errors.Right you are - my bad. Steve Jodi Showers 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-- Posted via http://www.ruby-forum.com/.
James Hughes
2006-Jun-01 19:13 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
On 6/1/06, Stephen Bartholomew <steve@2404.co.uk> 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).> James Hughes 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 Hughes > > 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"?'' > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- James Hughes 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 Bartholomew
2006-Jun-01 19:25 UTC
[Rails] Re: Re: @model.errors.empty? => true; @model.valid? => false
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 Hughes wrote:> On 6/1/06, Stephen Bartholomew <steve@2404.co.uk> 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 >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > James Hughes > 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"?''-- Posted via http://www.ruby-forum.com/.
James Hughes
2006-Jun-01 19:30 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
Figured it out. See below. On 6/1/06, James Hughes <hughes.james@gmail.com> wrote:> On 6/1/06, Stephen Bartholomew <steve@2404.co.uk> 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 Showers
2006-Jun-01 19:34 UTC
[Rails] Re: @model.errors.empty? => true; @model.valid? => false
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 On 1-Jun-06, at 3:13 PM, James Hughes wrote:> On 6/1/06, Stephen Bartholomew <steve@2404.co.uk> 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). > >> James Hughes 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 Hughes >> > 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"?'' >> >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > James Hughes > 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"?'' > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails