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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-07 12:35 UTC
Re: before_save :strip_whitespace => saves with spaces
On Feb 7, 7:11 am, Newb Newb <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> class CompanyInfo < ActiveRecord::Base > before_save :strip_whitespace > > def strip_whitespace > puts "strip_whitespace" > -sSU04LAzd1rjvMUfxMFAGA@public.gmane.org do |attr,value| > puts value > puts "before strip" > value = value.strip > puts "after strip" > puts value > end > > end > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. Could you tell me the way of implementation pls. How can i resolve it. Thanks in advance -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:11 am, Newb Newb
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
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
>
> class CompanyInfo < ActiveRecord::Base
> before_save :strip_whitespace
>
> def strip_whitespace
> puts "strip_whitespace"
> -sSU04LAzd1rjvMUfxMFAGA@public.gmane.org do |attr,value|
> puts value
> puts "before strip"
> value = value.strip
> puts "after strip"
> puts value
> end
>
> end
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Aug-05 23:12 UTC
Re: Re: before_save :strip_whitespace => saves with spaces
> 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.> end > end > > On Feb 7 2009, 12:11 am, Newb Newb <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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 >> >> 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 >> -- >> Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.