Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:45+00:00 2026-06-17T09:22:45+00:00

I’m a fairly new Rails developer, and I have an existing application that I’m

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:22:46+00:00Added an answer on June 17, 2026 at 9:22 am

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.