如何找到具有多个“has_one”关联的正确记录?

时间:2013-01-08 22:28:52

标签: ruby-on-rails ruby

我是一个相当新的Rails开发人员,我有一个现有的应用程序,我正在尝试向(Ability)添加一个与现有模型(Champion)关联的新模型。我在逻辑上以我理解“has_one”和“belongs_to”机制的方式设置了对我有意义的东西,但我只是为每个冠军中的每个has_one关系获得相同的一条记录。

以下是设置:

现有的模型叫做Champion,我正在创建一个新的Ability模型,它是Champion模型的一部分。我想组织冠军模型,使其具有5个名为“q”,“w”,“e”,“r”和“被动”的能力。每种能力都只属于一个冠军。

这是我的迁移,它为belongs_to关联添加了一个带有champion_id的新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

Ability.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

既然这一点我有一个似乎正在创建关系的模型,但是当我在服务器上查看输出时,它为每个能力关联执行相同的select语句,并选择每次记录相同。

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

2 个答案:

答案 0 :(得分:1)

在Champion模型中进行此操作。使用belongs_to,foreign_key将位于您定义关系的模型上,has_one将查看您关联的模型。

belongs_to :q, :class_name => "Ability", :foreign_key => "q" 
belongs_to :w, :class_name => "Ability", :foreign_key => "w"
belongs_to :e, :class_name => "Ability", :foreign_key => "e" 
belongs_to :r, :class_name => "Ability", :foreign_key => "r" 

More info

答案 1 :(得分:0)

你应该将foreign_key选项添加到冠军模型中:

has_one :q, :class_name => "Ability", :foreign_key => "q" 
has_one :w, :class_name => "Ability", :foreign_key => "w"
has_one :e, :class_name => "Ability", :foreign_key => "e" 
has_one :r, :class_name => "Ability", :foreign_key => "r"