I am new to ruby and writing a small script that requires writing messages to a database log.
I am using ruby 1.9.3 with active_record but without rails. All select statements work fine, but my write to log function is returning the following error:
DEPRECATION WARNING: You're trying to create an attribute `ID'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.
my model looks like that
class ActLog < ActiveRecord::Base
self.table_name = "ActLog"
self.primary_key = "ID"
end
and my log function:
def log(level, message)
level.upcase!
line = ActLog.new
line.level = level
line.message = message
line.module = 'script'
line.date = Time.new.strftime("%Y-%m-%d %H:%M:%S")
line.save
end
the ID
field is an int auto_increment.
The log function will be called many times during the execution. Is that the best way to write to the log and why is the deprecation warning popping up?
I checked the API and the methods you're using are current. I think you may need attr_accessor
class ActLog < ActiveRecord::Base
attr_accessor :id, :level, :message, :module, :date
self.table_name = "ActLog"
self.primary_key = "ID"
end
def log(level, message)
attr = { :level => level.upcase!,
:message => message,
:module => 'script',
:date => Time.new.strftime("%Y-%m-%d %H:%M:%S") }
ActLog.new(attr).save!
end
I think your class should be as follows:
class ActLog < ActiveRecord::Base
self.primary_key = 'ID'
self.table_name = 'ActLog'
end
Out of curiosity, why the non-standard names?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.