草庐IT

sonata_type_datetime_picker

全部标签

ruby - 如何在 Ruby 中使用 DateTime strptime 解析非英语日期?

我正在尝试解析从CMS导出的日期。不幸的是,瑞典语言环境设置。月份名称缩写为三个字符,这在May和October月份时有所不同(“maj”与“May”以及“okt”与“Oct”)。我希望使用具有正确区域设置的DateTime.strptime来解决这个问题,如下所示:require'locale'Locale.default="sv_SE"require'date'DateTime.strptime("10okt200904:32",'%d%b%Y%H:%M')然而,日期仍然被解析,因为它会使用英文缩写的月份名称:ArgumentError:invaliddatefromlib/rub

ruby-on-rails - 类型错误 : wrong argument type String (expected Module)

我有以下代码:classProfileLookup基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过Rails控制台,此代码按预期工作:ruby-1.9.3@hub:002>ProfileLookup.available_gendersProfileLookupLoad(0.6ms)SELECT"profile_lookups".*FROM"profile_lookups"WHERE"profile_lookups"."category"='gender'ORDERBYvalue=>["Female","Male"]但是,我的规范不合格。以下规范:requ

ruby - 自定义 to_yaml 和 domain_type

我需要定义用于序列化/反序列化对象的自定义方法。我想做类似下面的事情。classPersondefto_yaml_type"!example.com,2010-11-30/Person"enddefto_yaml"stringrepresentingperson"enddeffrom_yaml(yaml)Person.load_from(yaml)endend声明序列化/反序列化的正确方法是什么? 最佳答案 好的,这就是我想出的classPersondefto_yaml_type"!example.com,2010-11-30/pe

ruby - DateTime 解析未按预期工作

我的Ruby代码看起来像这样。str=2010-12-02_12-10-26putsstrputsDateTime.parse(str,"%Y-%m-%d_%H-%M-%S")我希望从解析中得到实际时间。相反,我得到这样的输出......2010-12-02_12-10-262010-12-02T00:00:00+00:00我如何获得解析的时间? 最佳答案 这个有效:str="2010-12-02_12-10-26"putsstrputsDateTime.strptime(str,"%Y-%m-%d_%H-%M-%S")这个例子在C

ruby-on-rails - 如何获得 DateTime 持续时间?

我很困惑如何做到这一点。我需要获取一个日期时间对象,并获取当前时间的持续时间(以小时、天等为单位)。谢谢。 最佳答案 获取以秒为单位的持续时间很容易:>>foo=Time.new=>MonDec2918:23:51+01002008>>bar=Time.new=>MonDec2918:23:56+01002008>>printbar-foo5.104063=>nil所以,五秒多一点。但要以更人性化的形式呈现它,您需要第三方添加,例如time_period_to_s,或Duration包。

ruby - 如何在 ruby​​ 中设置 header ['content-type' ] ='application/json'

require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby​​中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade

ruby-on-rails - Rails 将 DateTime 转换为 'where' 查询中的日期

我需要查询数据库并按开始日期过滤事件,但列类型是DateTime。我在模型中做了范围:scope:day,->(start_date){wherestart_date:start_date}对于相同的DateTime值,它工作正常,但我需要一个过滤器来仅按日期而不是DateTime获取Event。我有PG数据库并尝试:scope:day,->(start_date){where("start_date:date=?","#{start_date.to_date}")}但是我得到一个错误 最佳答案 你可以这样做:使用SQL日期函数(取

ruby - ruby Date.today 和 DateTime.now 的日期错误

我已经使用RVM安装了ruby​​-1.8.6-p383。系统ruby是1.9.1_p378-1我在使用ruby​​1.8时从Date.today和DateTime.now得到错误的日期。而Time.now是正确的:irb(main):002:0>DateTime.now.to_s=>"2126--1-10618T11:23:43+00:00"irb(main):004:0>Date.today.to_s=>"2126--1-10618"irb(main):005:0>Time.now=>ThuJan2811:55:27+00002010如果我切换到ruby​​1.9,一切都很好:ir

ruby - pg gem : 'Warning: no type cast defined for type "numeric"'

我在从pggem中获取输入结果时遇到问题。require'pg'require_relative'spec/fixtures/database'client=PG.connect(DB[:pg])client.type_map_for_queries=PG::BasicTypeMapForQueries.new(client)client.type_map_for_results=PG::BasicTypeMapForResults.new(client)client.exec(%|select*fromtestme;|)do|query|query.each{|r|putsr.ins

ruby-on-rails - Rails ActiveRecord - update_all : how to update multiple fields at once with mixed type of arguments

我有一个用例,我想使用ActiveRecord::Relationupdate_all方法并指定要设置的几个字段。我使用update_all是因为可以更新很多条目,我不想将它们全部加载并一个一个地更新。其中一些需要直接的SQLSET语句,例如因为我根据另一列的值设置一列。是否有一个简单的语法与update_all一起使它可读,沿着这个=>MyModel.where(state::foo).update_all(['timespent=timespent+500',#Can'tbeanythingelsethanaSQLstatementstate::bar,#RegularRailsS