0

In Rails 3.x I have a model class with an int field "my_int_date". I would like the int field to be populated by a form which as a date_select form element.

The problem is when the form reads a date_select value it sends it back as a multi-parameter value which results in the error

1 error(s) on assignment of multiparameter attributes

Is there anyway I can keep it stored as a Date in my model but convert it to an int when it is dumped to the DB?

bgura
  • 860
  • 15
  • 33

2 Answers2

0

Ruby can convert a Time object into seconds with to_i

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

If it is a string, then you can use the method to_time

'2012-06-01'.to_time.to_i # string to Time object then to epoc time (s)
Rob Taylor
  • 604
  • 4
  • 10
  • Thanks but I am aware of that conversion :/. There is one way I am aware of to parse the date_select to a dateTime but it looks messy for something that should be so common. See this post http://stackoverflow.com/questions/5073756/where-is-the-rails-method-that-converts-data-from-datetime-select-into-a-datet – bgura Aug 07 '12 at 02:40
0

Heres how I solved it,

In my ActiveRecord model I added the fields

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

In my form I then linked the datetime field to the field my_int_date_time rather than linking it to my_int_date

bgura
  • 860
  • 15
  • 33