【发布时间】:2010-05-06 16:50:49
【问题描述】:
我有以下模仿我的问题的 ActiveRecord 测试用例。我有一个 People 表,其中一个属性是日期。我在该表上创建了一个视图,添加了一列,即该日期加上 20 分钟:
#!/usr/bin/env ruby
%w|pp rubygems active_record irb active_support date|.each {|lib| require lib}
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "test.db"
)
ActiveRecord::Schema.define do
create_table :people, :force => true do |t|
t.column :name, :string
t.column :born_at, :datetime
end
execute "create view clowns as select p.name, p.born_at, datetime(p.born_at, '+' || '20' || ' minutes') as twenty_after_born_at from people p;"
end
class Person < ActiveRecord::Base
validates_presence_of :name
end
class Clown < ActiveRecord::Base
end
Person.create(:name => "John", :born_at => DateTime.now)
pp Person.all.first.born_at.class
pp Clown.all.first.born_at.class
pp Clown.all.first.twenty_after_born_at.class
问题是,输出是
Time
Time
String
当我期望视图的新 datetime 属性也是 ruby 世界中的 Time 或 DateTime 时。有什么想法吗?
我也试过了:
create view clowns as select p.name, p.born_at, CAST(datetime(p.born_at, '+' || '20' || ' minutes') as datetime) as twenty_after_born_at from people p;
结果相同。
【问题讨论】:
标签: ruby activerecord sqlite types