Forum: Ruby on Rails Something as after_successful_validation?

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.
stuff (Guest)
on 2006-01-30 12:42
I need to call callback only if the validation was successful, but it
seems it is called also when validation fails.
Gerret A. (Guest)
on 2006-01-30 12:51
(Received via mailing list)
How about

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

cheers
Gerret
Jeroen H. (Guest)
on 2006-01-30 12:57
(Received via mailing list)
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
Pat M. (Guest)
on 2006-01-30 13:10
(Received via mailing list)
This is the default behavior in AR.  AR objects evaluate a zero-length
string as nil.

Pat
Jeroen H. (Guest)
on 2006-01-30 13:41
(Received via mailing list)
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
Jonathan V. (Guest)
on 2006-01-30 14:40
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
Andrew S. (Guest)
on 2006-01-30 15:49
(Received via mailing list)
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....
Jeroen H. (Guest)
on 2006-01-30 15:52
(Received via mailing list)
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
Andrew S. (Guest)
on 2006-01-30 16:33
(Received via mailing list)
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
Jeroen H. (Guest)
on 2006-01-30 16:52
(Received via mailing list)
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 :-)

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


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