我是ruby的新手并编写了一个小脚本,需要将消息写入数据库日志。
我使用ruby 1.9.3和active_record但没有rails。所有select语句都可以正常工作,但是我的写入日志函数返回了以下错误:
DEPRECATION WARNING: You're trying to create an attribute `ID'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.
我的模型看起来像那样
class ActLog < ActiveRecord::Base
self.table_name = "ActLog"
self.primary_key = "ID"
end
和我的日志功能:
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
ID
字段是int auto_increment。
执行期间将多次调用日志函数。这是写入日志的最佳方式,为什么会出现弃用警告?
答案 0 :(得分:1)
我检查了the API,您使用的方法是最新的。我想你可能需要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
答案 1 :(得分:0)
我认为你的课程应该如下:
class ActLog < ActiveRecord::Base
self.primary_key = 'ID'
self.table_name = 'ActLog'
end
出于好奇,为什么非标准名称?