On Tuesday, 25 June 2013 05:15:06 UTC-7, Ruby-Forum.com User wrote:
>
> Catching that 0.0 value was the right direction to look in: 
>
>   def convert_hours 
>     if hours_before_type_cast && hours_before_type_cast =~ /:/ 
>       hrs, mins = hours_before_type_cast.scan( /(\d*):(\d*)/ ).first 
>       self.hours = BigDecimal( ( hrs.to_f + ( mins.to_f / 60.0 ) ), 4 ) 
>     end 
>   end 
>
>
A cleaner way to do this would be to override the setter for hours:

def hours=(value)
  if value =~ /:/
    converted_value = ... # do the conversion here
    super(converted_value)
  else
    super
  end
end

As an upside, this means that setting hours will *always* return the 
correct value, even before the before_validation callback runs.

--Matt Jones
end 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/366c0080-73bf-48c9-8812-db695881de35%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to