Forum: Ruby on Rails before_save :strip_whitespace => saves with spaces

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.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-02-07 08:11
i used this private function for removing leading and trailing white
spaces from the values.that below function would be called before_save.
when i print the value after it strips.it prints string without any
spaces.but in the table fields it saves with spaces .
waht would be the problem.pls help me


class CompanyInfo < ActiveRecord::Base
before_save :strip_whitespace

def strip_whitespace
     puts "strip_whitespace"
     @attributes.each do |attr,value|
         puts value
         puts "before strip"
         value = value.strip
         puts "after strip"
         puts value
     end









   end
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-07 13:35
(Received via mailing list)
On Feb 7, 7:11 am, Newb Newb <rails-mailing-l...@andreas-s.net> wrote:
> i used this private function for removing leading and trailing white
> spaces from the values.that below function would be called before_save.
> when i print the value after it strips.it prints string without any
> spaces.but in the table fields it saves with spaces .
> waht would be the problem.pls help me
>

because when you do

value = value.strip

you're not stripping whitespace from value. You are creating a new
string object that has been stripped. The @attributes hash still
contains the unstripped version.

Fred
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-02-10 05:19
Thanks for the reply.
Could you tell me the way of implementation pls.
How can i resolve it.


Thanks in advance
0c31801aba0747b84974df56ade208f3?d=identicon&s=25 rob doan (Guest)
on 2009-03-01 23:49
(Received via mailing list)
Newb Newb wrote:
> Thanks for the reply.
> Could you tell me the way of implementation pls.
> How can i resolve it.
>
>
> Thanks in advance
>

change line
  value = value.strip
to:
  self[attr] = value.strip
8ccf3d9a746250a8b7ca8d605f73bf39?d=identicon&s=25 Todd Sedano (Guest)
on 2011-08-05 19:22
(Received via mailing list)
I'm updating this older thread based upon my recent experience. This
will not call strip on nils, fixnums, etc.

  before_save :strip_whitespace

  def strip_whitespaces
    @attributes.each do |attr,value|
      self[attr] = value.strip if value.is_a?(String)
    end
  end

On Feb 7 2009, 12:11am, Newb Newb <rails-mailing-l...@andreas-s.net>
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2011-08-06 01:14
(Received via mailing list)
> I'm updating this older thread based upon my recent experience. This
> will not call strip on nils, fixnums, etc.
>
>  before_save :strip_whitespace
>
>  def strip_whitespaces
>    @attributes.each do |attr,value|
>      self[attr] = value.strip if value.is_a?(String)

Might also try...

      self[attr] = value.strip if value.respond_to?(:strip)

... to pick up things that aren't strings, but are string like.
This topic is locked and can not be replied to.