DataMapper NoMethodError:DateTime的未定义方法`encode_json'

时间:2010-11-27 16:58:53

标签: 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', :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返回一个数组 结构如下:

#<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>

当我尝试Match.find_matches.to_json时,似乎to_json在begin_date格式上窒息。我能做些什么来纠正这个问题吗?我正在使用MySQL 5.1 DATETIME,它通常显示为yyyy-mm-dd hh:mm:ss(至少在MySQL客户端中),所以有些东西以某种方式格式化 encode_json无法消化。

1 个答案:

答案 0 :(得分:0)

这似乎是ActiveSupport :: JSON.encode中的错误。不支持Struct类型。我打开了一张票并提供了解决方法。 https://rails.lighthouseapp.com/projects/8994/tickets/6077-activesupportjsonencode-fails-for-struct-types#ticket-6077-1