Ruby + ActiveRecord: How to configure a date field resulting in 'yyyy-mm-dd' date format? -
i using activerecord 4.2.4 work database.
some background:
- ruby '1.9.3'
- activerecord '4.2.4'
- cucumber '2.1.0'
- i not using rails.
i ran issue, output file generated shows:
role | updated_date teacher | 2016-10-26 09:54:06 utc
but not want '2016-10-26 09:54:06 utc' in updated_date, expect '2016-10-26' show in file, below:
role | updated_date teacher | 2016-10-26
the updated_date column has type of date in table. execute query without converting anything. , not using ruby object/model map database table, instead run sql , print out results file. here have in code:
def initialize activerecord::base.time_zone_aware_attributes = false db_config = dbconfig.get_config_file(db_config) activerecord::base.establish_connection( :adapter => db_config['adapter'], :username => db_config['username'], :password => db_config['password'], :database => db_config['database'] ) @connection = activerecord::base.connection end def extract_file delimiter = '|' output_file = 'extract.txt' query = "select role, updated_date role user_id = 'user1'" # loop through result file.open(output_file, 'w'){|f| result = @connection.exec_query(query) result.rows.each |row| array << (row.join(delimiter)) if array.length >= 100 f.puts array.join("\n") array = [] end end end
i tried add line below:
activerecord::timestamp.record_timestamps = false
however, got following error:
undefined method `record_timestamps=' activerecord::timestamp:module
according site:
http://www.rubydoc.info/gems/activerecord/4.2.4/activerecord/timestamp timestamping can turned off setting: config.active_record.record_timestamps = false
where configure it? in config/database.yml file? please share solutions.
when interested in date of last update , not time, there 2 option:
- don't use
updated_at
timestamp
column in database. instead useupdated_on
date
column , rails magic rest. or - cast
timestamp
date
data type before writing output file. simpleupdated_at.to_date
job.
Comments
Post a Comment