Mysql DataMapper NoMethodError:未定义的方法'encode_json';日期时间
我不确定这是DataMapper问题还是Rails特有的问题: NoMethodError:Sun的未定义方法“encode_json”,2010年11月28日00:00:00-0500:DateTime 我有一个定义如下的模型:Mysql DataMapper NoMethodError:未定义的方法'encode_json';日期时间,mysql,ruby-on-rails,datetime,datamapper,Mysql,Ruby On Rails,Datetime,Datamapper,我不确定这是DataMapper问题还是Rails特有的问题: NoMethodError:Sun的未定义方法“encode_json”,2010年11月28日00:00:00-0500:DateTime 我有一个定义如下的模型: class Match include DataMapper::Resource storage_names[:default] = 'matches' property :id, Serial, :field => 'matches_id', :req
class Match
include DataMapper::Resource
storage_names[:default] = 'matches'
property :id, Serial, :field => 'matches_id', :required => true
def self.find_matches
repository.adapter.select <<-SQL
select
m.matches_id,
m.begin_date,
m.windows_time_zone,
team1_id,
team2_id,
t1.name as team1,
t2.name as team2,
m.status,
COALESCE(md.name,'') as match_day_name,
s.name as season_name,
r.name as round_name,
c.name as competition_name,
COALESCE(g.name,'') as group_name,
COALESCE(m.use_gsm,'N') as use_gsm,
COALESCE(m.team1_finalscore,0) as team1_score,
COALESCE(m.team2_finalscore,0) as team2_score
from matches m
inner join round r on (r.round_id = m.round_id)
inner join season s on (s.season_id = r.season_id)
inner join competition c on (c.competition_id =
s.competition_id)
inner join team t1 on (t1.team_id = m.team1_id)
inner join team t2 on (t2.team_id = m.team2_id)
left outer join match_day md on (m.match_day_id =
md.match_day_id)
left outer join groups g on (r.round_id = g.round_id)
where m.begin_date <= (CURDATE() + interval 1 day)
and m.begin_date >= (CURDATE() - interval 1 day)
order by m.begin_date desc
SQL
end
end
当我尝试Match.find_matches.to_json
时,to_json
似乎被begin_日期格式阻塞了。我能做些什么来纠正这个问题吗?我使用的是MySQL 5.1 DATETIME,它通常显示为yyyy-mm-dd-hh:mm:ss(至少在MySQL客户机中是这样),因此某种格式化方式
encode_json无法消化。这似乎是ActiveSupport::json.encode中的一个错误。不支持结构类型。我开了一张罚单,提供了一个解决办法 “Match.find.matches.begin_date.class”报告是什么?它报告日期时间。我找到了我认为是根本原因的东西:ActiveSupport:JSON.encode不支持结构。我运行的是ruby 1.8.7,没有这个问题
Struct.new(:d).new(DateTime.parse(Time.now.to_s(:db))[0].to_json
据我所知,这是一个ActiveSupport:json.encode问题,不是ruby问题。
#<struct matches_id=54235, begin_date=Sun, 28 Nov 2010 00:00:00 -0500,windows_time_zone="Morocco Standard Time", team1_id=937, team2_id=943,team1="Deportes Tolima", team2="La Equidad", status="N",match_day_name="Cuadrangular Semifinal Fecha 3",
season_name="Finalizacion 2010", round_name="Cuadrangular Semifinal",competition_name="Liga Postobon - Colombia", group_name="",use_gsm="N", team1_score=0, team2_score=0>