Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails将日期转换为int值_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails Rails将日期转换为int值

Ruby on rails Rails将日期转换为int值,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,在Rails3.x中,我有一个带有int字段“my_int_date”的模型类。我希望int字段由一个表单填充,该表单作为date\u选择表单元素 问题是,当表单读取日期选择值时,它会将其作为多参数值发送回,从而导致错误 1 error(s) on assignment of multiparameter attributes 我是否可以将其作为日期存储在模型中,但在转储到DB时将其转换为int?Ruby可以使用to_I将时间对象转换为秒 Time.now.to_i # Returns epo

在Rails3.x中,我有一个带有int字段“my_int_date”的模型类。我希望int字段由一个表单填充,该表单作为date\u选择表单元素

问题是,当表单读取日期选择值时,它会将其作为多参数值发送回,从而导致错误

1 error(s) on assignment of multiparameter attributes

我是否可以将其作为日期存储在模型中,但在转储到DB时将其转换为int?

Ruby可以使用to_I将时间对象转换为秒

Time.now.to_i # Returns epoc time (s)
如果它是一个字符串,那么您可以使用该方法来计算时间

'2012-06-01'.to_time.to_i # string to Time object then to epoc time (s)
我就是这样解决的

在我的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

在我的表单中,我将datetime字段链接到my_int_date_time字段,而不是链接到my_int_date

谢谢,但我知道这种转换:/。我知道有一种方法可以将date_select解析为dateTime,但对于一些本应如此常见的东西来说,它看起来很混乱。看到这个帖子了吗