Rails belongs_to返回nil

时间:2014-06-08 19:39:31

标签: ruby-on-rails json belongs-to rails-api

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

我确信这对我来说是在忽视一些事情,但我很茫然。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

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

 belongs_to :tournament, classname: 'Tourn', foreign_key: 'tourn'