【发布时间】:2016-11-04 19:51:28
【问题描述】:
在从 api 保存我的对象之前,我想将 unix 时间转换为人类时间。
但是我无法访问我的方法format date,这让我很兴奋:
1467738900000:Fixnum 的未定义方法 `format_date'
我的模特:
class Conference < ActiveRecord::Base
validates_presence_of :title, :date
validates :date, :uniqueness => true
def self.save_conference_from_api
data = self.new.data_from_api
self.new.parisrb_conferences(data).each do |line|
conference = self.new
conference.title = line['name']
conference.date = line['time'].format_date
conference.url = line['link']
if conference.valid?
conference.save
end
end
self.all
end
def format_date
DateTime.strptime(self.to_s,'%Q')
end
【问题讨论】:
-
正如错误提示的那样,
line['time']是一个数字,而不是您的类Conference的实例。原因不在您发布的代码中。
标签: ruby-on-rails ruby activerecord methods rails-activerecord