Something as after_successful_validation?

I need to call callback only if the validation was successful, but it
seems it is called also when validation fails.

How about

class Item < AR:B
def after_validation
if valid?
cheer :immediately
end
end
end

cheers
Gerret

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

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 H. wrote:

Pat M. 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

Pat M. 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 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…

Jonathan V. 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
end

It’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 S. 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 == 0

Works great! My final version :slight_smile:

def empty_to_null
attributes.each do|key, value|
self[key] = nil if value.kind_of?(String) and value.length == 0
end
end

Jeroen