我是一个相当新的 Rails 开发人员,我有一个现有的应用程序,我正在尝试向(Ability)添加一个新模型,该模型与现有模型(Champion)有关联。按照我理解“has_one”和“belongs_to”机制的方式,我将它设置为对我来说在逻辑上有意义的方式,但是对于每个冠军中的每个 has_one 关系,我只能获得相同的一个记录。
这是设置:
现有模型称为 Champion,我正在创建一个新的 Ability 模型,它是 Champion 模型的一部分。我想组织 Champion 模型,使其具有 5 个能力,分别名为“q”、“w”、“e”、“r”和“passive”。每种能力只属于一个英雄。
champion_id
这是我的迁移,它为 belongs_to 关联添加了一个新的 Ability 对象:
class CreateAbilities < ActiveRecord::Migration
def change
create_table :abilities do |t|
t.integer :champion_id
t.timestamps
end
end
end
以下是将能力引用添加到 Champion 模型的迁移:
class AddAbilitiesToChampions < ActiveRecord::Migration
def change
add_column :champions, :q, :integer
add_column :champions, :w, :integer
add_column :champions, :e, :integer
add_column :champions, :r, :integer
add_column :champions, :passive, :integer
end
end
能力.rb模型:
class Ability < ActiveRecord::Base
attr_accessible :ability_description,
:cost,
:effect,
:cooldown,
:range,
:name,
:icon,
:champion_id
belongs_to :champion
end
Champion.rb 模型:
class Champion < ActiveRecord::Base
attr_accessible :champion_id,
:q,
:w,
:e,
:r,
:passive
has_one :q, :class_name => "Ability"
has_one :w, :class_name => "Ability"
has_one :e, :class_name => "Ability"
has_one :r, :class_name => "Ability"
has_one :passive, :class_name => "Ability"
end
现在,我有一个似乎可以正确创建关系的模型,但是当我在服务器上查看输出时,它为每个能力关联执行相同的选择语句,并且每次都选择相同的记录。
Started GET "/champions.json" for 127.0.0.1 at 2013-01-08 15:13:30 -0700
Processing by ChampionsController#index as JSON
Champion Load (0.5ms) SELECT "champions".* FROM "champions"
Item Load (0.4ms) SELECT "items".* FROM "items"
Spell Load (0.3ms) SELECT "spells".* FROM "spells"
Mastery Load (0.3ms) SELECT "masteries".* FROM "masteries"
CACHE (0.0ms) SELECT "champions".* FROM "champions"
Ability Load (0.6ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 1 LIMIT 1
Ability Load (0.2ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 2 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 2 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 2 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 2 LIMIT 1
CACHE (0.0ms) SELECT "abilities".* FROM "abilities" WHERE "abilities"."champion_id" = 2 LIMIT 1
Completed 200 OK in 111ms (Views: 47.4ms | ActiveRecord: 12.8ms)
如何修改我的设置,以便 select 语句为这些属性中的每一个选择正确的能力?我已经尝试过反转关系但没有取得多大成功。任何帮助表示赞赏!
编辑:这是我的 Champions_controller.rb 中的代码,它为模型提供 JSON:
class ChampionsController < ApplicationController
before_filter :find_champions
def find_champions
@champions = Champion.all
end
def index
respond_to do |format|
format.json { render :json => { "champions" => @champions }}
end
end
end