I need to call callback only if the validation was successful, but it seems it is called also when validation fails. -- Posted via http://www.ruby-forum.com/.
How about class Item < AR:B def after_validation if valid? cheer :immediately end end end cheers Gerret On 1/30/06, stuff <kontakt@daeltar.org> wrote:> I need to call callback only if the validation was successful, but it > seems it is called also when validation fails. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi, When I update a record using a form and leave a nullable varchar field empty, the SQL generated looks like this: UPDATE table SET field='''' instead of: UPDATE table SET field=NULL This shouldn''t be the default behaviour, right? I''m using postgresql 8.1.1 TIA, Jeroen
This is the default behavior in AR. AR objects evaluate a zero-length string as nil. Pat On 1/30/06, Jeroen Houben <jeroen@terena.nl> wrote:> Hi, > > When I update a record using a form and leave a nullable varchar field > empty, the SQL generated looks like this: > > UPDATE table SET field='''' > > instead of: > > UPDATE table SET field=NULL > > This shouldn''t be the default behaviour, right? > I''m using postgresql 8.1.1 > > TIA, > > Jeroen > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Pat Maddox wrote:> This is the default behavior in AR. AR objects evaluate a zero-length > string as nil.As nil. So shouldn''t nil be converted to SQL NULL? That seems more intuitive to me. Anyway, so if I need different behavior, the easiest way is to write some before_save code? Jeroen
I guess if you wanted null to be used you could do something like this (untested): def before_save attributes.each do |key, value| attributes[key] = nil if value.blank? end end My guess is that would use null instead of '''' in the sql, but I could be wrong. -Jonny. Jeroen Houben wrote:> Pat Maddox wrote: >> This is the default behavior in AR. AR objects evaluate a zero-length >> string as nil. > > As nil. So shouldn''t nil be converted to SQL NULL? That seems more > intuitive to me. > > Anyway, so if I need different behavior, the easiest way is to write > some before_save code? > > Jeroen-- Posted via http://www.ruby-forum.com/.
I ran into a problem with this behaviour while trying to use validates_length_of. There''s a bug (#3588) that I submitted. It wouldn''t allow me to use: validates_length_of :social_security, :is => 9, :allow_nil => true This is because the string was always being converted to '''' instead of remaining nil. So I wrote this plugin: ###############begin plugin module Stone module Tweaks def self.included(base) base.send :include, Stone::Tweaks::InstanceMethods base.before_validation{|model| model.nil_empty_fields } end module InstanceMethods def nil_empty_fields self.attributes.each{|key, value| if not value.nil? and value.kind_of?(String) self[key] = value if value.length == 0 end } end end#InstanceMethods end end#Tweaks end end#Stone end ActiveRecord::Base.send :include, Stone::Tweaks ###############end plugin This does the trick for me.... -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060130/b072bef4/attachment.html
Jonathan Viney wrote:> I guess if you wanted null to be used you could do something like this > (untested): > > def before_save > attributes.each do |key, value| > attributes[key] = nil if value.blank? > end > endIt''s not working, but I don''t know why. Code looks good to me. This is a bit weird, taken from a breakpoint/irb session irb(#<Player:0xb789a2b0>):004:0> attributes[''ph_home''] => "" irb(#<Player:0xb789a2b0>):005:0> attributes[''ph_home''].empty? => true irb(#<Player:0xb789a2b0>):006:0> attributes[''ph_home''] = nil => nil irb(#<Player:0xb789a2b0>):007:0> attributes[''ph_home''] Any ideas?? Jeroen
doh!, forgot I had changed this...(I need to change the bug post) self[key] = value if value.length == 0 ----should be--- self[key] = nil if value.length == 0 -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060130/8ce6dabf/attachment.html
Andrew Stone wrote:> doh!, forgot I had changed this...(I need to change the bug post) > > self[key] = value if value.length == 0 > > ----should be--- > > self[key] = nil if value.length == 0Works great! My final version :-) def empty_to_null attributes.each do|key, value| self[key] = nil if value.kind_of?(String) and value.length == 0 end end Jeroen