0

api에서 내 객체를 저장하기 전에 유닉스 시간을 휴먼 시간으로 변환하고 싶습니다. 그러나 나는 내 방법 format date에 액세스 할 수 없습니다, 그것은 나를 높이 :레일즈 : 모델에서 정의되지 않은 메소드

정의되지 않은 메서드`format_date 1,467,738,900,000에 대한 : Fixnum이라는

에게 내 모델 :

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 
+0

오류가 암시 하듯이'line [ 'time']'은 숫자이며,'Conference' 클래스의 인스턴스가 아닙니다. 그 이유는 게시 한 코드가 아닙니다. –

답변

1

line['time']이 인스턴스가 아닌 Conference 클래스 중 하나이므로 format_date 메서드를 호출 할 수 없습니다. 다음과 같이 호출 다음

def self.format_date str 
    DateTime.strptime(str.to_s,'%Q') 
end 

그리고 :

conference.date = format_date(line['time']) 

다른 옵션과 같이 될 것입니다 before_validation 콜백 (속성 할당을 사용하는 대신에, 예를 들어, 당신은 format_date에게 수업 방법을 만들 수 있습니다 conference.date = line['time']과)format_date 방법에 대한 필요가 없습니다 : 다음

before_validation -> r { r.date = DateTime.strptime(r.date.to_s,'%Q') } 
1

유닉스 시간 (밀리 초)으로 날짜가 표시됩니다. 이런 식으로 할 수있다

conference.date = DateTime.strptime(line['time'].to_s,'%Q') 
+0

그것은 첫 번째 아이디어 였지만 그 방법을 사용하고 싶었습니다. – Orsay

관련 문제