我正在为现有数据集开发 api 以开发移动应用程序。数据库对我来说是不可变的,因为使用的 Web 应用程序正在生产中。使用rails-api和 JSON 渲染得很好。Tourn
对象将正确返回一个事件列表,这很好。事件将返回我想要的大部分信息,但belongs_to
tourn
关系正在返回nil
。
我想要的是与请求的事件相关联的锦标赛对象的 JSON 表示。
#event.rb
class Event < ActiveRecord::Base
self.table_name = 'event'
self.inheritance_column = :ruby_type
#as written below, this causes the stack too deep/500 error
#without this, json returned = {"id":22185,"name":"Policy Debate","fee":155.0,"tourn":2299}
#with :primary_key => 'tourn', or any other key i get {"id":22185,"name":"Policy Debate","fee":155.0,"tourn":null}
belongs_to :tourn, :foreign_key => 'tourn'
end
#tourn.rb
class Tourn < ActiveRecord::Base
self.table_name = 'tourn'
has_many :events, :foreign_key => 'tourn', :class => :event
end
#events_controller.rb
class EventsController < ApplicationController
...
def show
@event = Event.find(params[:id])
render json: @event, root: false
end
mysql> describe event;
+---------------+--------------+------+-----+
| Field | Type | Null | Key |
+---------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| tourn | int(11) | YES | MUL | --`id` of a tournament; cannot be changed in db
| name | varchar(127) | NO | |
| type | varchar(15) | YES | |
| abbr | varchar(11) | YES | |
| event_double | int(11) | YES | |
| fee | float | YES | |
| timestamp | timestamp | NO | |
| judge_group | int(11) | YES | |
| rating_subset | int(11) | YES | |
+---------------+--------------+------+-----+
10 rows in set (0.01 sec)
# mongrel output
# stack too deep/500 error
Started GET "/event/22185" for 127.0.0.1 at 2014-06-08 12:12:18 -0500
Processing by EventsController#show as HTML
Parameters: {"id"=>"22185"}
Event Load (0.2ms) SELECT `event`.* FROM `event` WHERE `event`.`id` = 22185 LIMIT 1 # this is wrong
Tourn Load (0.3ms) SELECT `tourn`.* FROM `tourn` WHERE `tourn`.`id` = 2299 LIMIT 1 #this is correct
Event Load (44.3ms) SELECT `event`.* FROM `event` WHERE `event`.`tourn` = 2299
CACHE (0.0ms) SELECT `tourn`.* FROM `tourn` WHERE `tourn`.`id` = 2299 LIMIT 1 [["id", 2299]]
...
#repeated a few hundred times
CACHE (0.0ms) SELECT `event`.* FROM `event` WHERE `event`.`tourn` = 2299 [["tourn", 2299]]
CACHE (0.0ms) SELECT `tourn`.* FROM `tourn` WHERE `tourn`.`id` = 2299 LIMIT 1 [["id", 2299]]
CACHE (0.0ms) SELECT `event`.* FROM `event` WHERE `event`.`tourn` = 2299 [["tourn", 2299]]
CACHE (0.0ms) SELECT `tourn`.* FROM `tourn` WHERE `tourn`.`id` = 2299 LIMIT 1 [["id", 2299]]
Completed 500 Internal Server Error in 559ms
# rails console
Loading development environment (Rails 4.1.1)
2.1.2 :001 > e=Event.find(21499)
Event Load (0.4ms) SELECT `event`.* FROM `event` WHERE `event`.`id` = 21499 LIMIT 1
=> #<Event id: 21499, tourn: 2166, name: "Open Parliamentary Debate", type: "wudc", abbr: "OPD", event_double: nil, fee: nil, timestamp: "2013-07-05 20:20:24", judge_group: 5639, rating_subset: nil>
2.1.2 :002 > e.tourn
=> nil
# env
$ rails -v
Rails 4.1.1
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
我相信这对我来说忽略了一些东西,但我不知所措。任何帮助表示赞赏。