I’m a fairly new Rails developer, and I have an existing application that I’m trying to add a new model to (Ability) that has an association to an existing model (Champion). I have it setup to what makes sense to me logically the way I understand the “has_one” and “belongs_to” mechanics, but I’m only getting the same one record back for each has_one relationship in each Champion.
Here’s the setup:
The existing model is called Champion, and I’m creating a new Ability model that is part of the Champion model. I want to organize the Champion model so that it has 5 abilities named “q”, “w”, “e”, “r”, and “passive”. Each ability will only ever belong to one champion.
Here’s my migration that adds a new Ability object with a champion_id for the belongs_to association:
class CreateAbilities < ActiveRecord::Migration
def change
create_table :abilities do |t|
t.integer :champion_id
t.timestamps
end
end
end
Here is the migration that adds the ability references to the Champion model:
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 model:
class Ability < ActiveRecord::Base
attr_accessible :ability_description,
:cost,
:effect,
:cooldown,
:range,
:name,
:icon,
:champion_id
belongs_to :champion
end
Champion.rb model:
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
Now that this point I have a model that seems to be creating the relationships correctly, however when I view the output on the server its performing the same select statement for every ability association, and selecting the same record each time.
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)
How do I modify my setup so the select statement chooses the correct ability for each of these attributes? I’ve tried the inverting the relationship without much success. Any help is appreciated!
EDIT: Here’s the code in my champions_controller.rb which serves the JSON for the model:
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
Give this a go in the Champion model. With belongs_to the foreign_key will be on the model you define the relationship in, has_one will look in the model you associate with.
More info.