On Sat, Jan 21, 2012 at 4:30 AM, tashfeen.ekram <tashfeen.ek...@gmail.com>wrote:
> i am using before validate to format my input from my view file for > the columns of type DateTime. I have pasted the code for the > validation and works well for creating a new record. However, when > trying to update the record where the columns from and to (of type > DateTime) are null, then they are not set in the update call. It seems > like i am checking to see if the attribute is present and then does > not find one (i guess it looks in the prior saved values in the db > which is indeed empty?) even though on is sent in the update request. > > before_validation do > self.from = Time.parse(from.to_s) if attribute_present?("from") > self.to = Time.parse(to.to_s) if attribute_present?("to") > #puts "hello" > end > > I think a "before_validation" is not the correct place to do this input format conversion. I would suggest to implement your requirement with code like this (using a custom setter for those 2 fileds). (untested, pseudo code): def from=(time_string) write_attribute(:from, parse_time(time_string)) end def to=(time_string) write_attribute(:to, parse_time(time_string)) end def parse_time(time_string) Time.parse(time_string) end private :parse_time HTH, Peter -- Peter Vandenabeele http://twitter.com/peter_v http://rails.vandenabeele.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@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.