0

Rails 3.x에서 int 필드 "my_int_date"가있는 모델 클래스가 있습니다. int 필드를 date_select 양식 요소로 채워지는 양식으로 채우고 싶습니다. 형태가 오류Rails date_select를 int 값으로 변환

1 error(s) on assignment of multiparameter attributes 

결과 다중 매개 변수 값으로 다시 보내는 date_select 값 어쨌든 있나요 읽고 난 내 모델의 날짜로 저장 유지할 수있을 때

문제는 하지만 DB로 덤프 될 때 int로 변환할까요? 이 문자열 인 경우

답변

0

를 사용할 수 to_i

Time.now.to_i # Returns epoc time (s) 

와 함께 초에 Time 객체를 변환 할 수 있습니다

내 ActiveRecord 모델에서 필드를 추가했습니다.

attr_accessible :my_int_date_time 

columns_hash["my_int_date_time"] = ActiveRecord::ConnectionAdapters::Column.new("my_int_date_time", nil, "DateTime") 

def my_int_date_time 
    return TimeHelper.datetime_from_integer(self.my_int_date) 
end 

def my_int_date_time= val 
    self.my_int_date = TimeHelper.datetime_to_integer(val) 
end 

내 양식에서 my_int_date에 연결하지 않고 datetime 필드를 my_int_date_time 필드에 연결했습니다.

0

루비, 나는 그것을 해결하는 방법을 다음 Heres는 방법의 TO_TIME

'2012-06-01'.to_time.to_i # string to Time object then to epoc time (s) 
+0

감사하지만 그 변환을 알고 있습니다 : /. date_select를 dateTime으로 구문 분석하는 것을 알고있는 한 가지 방법이 있지만 너무 일반적 일 것입니다. 이 게시물보기 http://stackoverflow.com/questions/5073756/where-is-the-rails-method-that-converts-data-from-datetime-select-into-a-datet – bgura