vendredi 17 juillet 2015

How Type casting works in ruby on rails?

I am new to ruby on rails, I am writing a class Activity < ActiveRecord::Base. I am stuck at point where I need to convert an array of strings to integers.

The code is as below

#code from line 151

start_time_parts=start_time.split(":").collect{ |y| y.to_i }

start_time=Time.utc(2015,"jan",1,start_time_parts[0],start_time_parts[1],start_time_parts[2])
end_time_parts=end_time.split(":").collect{ |y| y.to_i }
end_time=Time.utc(2015,"jan",1,end_time_parts[0],end_time_parts[1],end_time_parts[2])

sql1='SELECT `Start_Time` FROM Times'
sql2='SELECT Rates FROM New_billcodes_mock WHERE Category_name=?, Instrument_name=? and Time_zone_name=?'


database_string_start_time =Activity.find_by_sql([sql1,'1'])
#error is here    
string_start_time_parts=database_string_start_time.split(":").collect{ |y| Activity.type_cast(y)}
database_start_time=Time.utc(2015,"jan",1,string_start_time_parts[0],string_start_time_parts[1],string_start_time_parts[2])

I get the error message as

NoMethodError (undefined method `type_cast' for #<Class:0x4c25340>):

Apparently I cannot use to_i method as it is not a method of the class Activity. Please help me if there is an existing method that I can use to convert to integers.

Aucun commentaire:

Enregistrer un commentaire