0

我正在为现有数据集开发 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]

我相信这对我来说忽略了一些东西,但我不知所措。任何帮助表示赞赏。

4

1 回答 1

2

我很确定您的问题是,外键和关联模型的数据库字段具有相同的名称。数据库字段由 ActiveRecord 在模型中作为名为“tourn”的成员方法提供,这显然与您的 belongs_to 定义相冲突,该定义还创建了一个名为“tourn”的成员方法。尝试以不同的方式调用您的模型,至少在关联中,使用

 belongs_to :tournament, classname: 'Tourn', foreign_key: 'tourn'
于 2014-06-08T21:43:13.640 回答