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

  • Home
  • SEARCH
  • 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 9183679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:51:51+00:00 2026-06-17T18:51:51+00:00

Not sure if I’m doing this right. Have two models: class Team < ActiveRecord::Base

  • 0

Not sure if I’m doing this right. Have two models:

class Team < ActiveRecord::Base
  attr_accessible :city, :name, :division_id
  belongs_to :divison
  has_many :players
end

class Division < ActiveRecord::Base
  attr_accessible :name
  has_many :teams
end

And the migration for :teams includes a foreign_key

create_table :teams do |t|
  t.string :name
  t.string :city
  t.integer :division_id
  t.foreign_key :division, dependent: :delete

  t.timestamps
end

In my Teams index view, I’d like to add the division name for each team. Getting the id is simple, I just use <%= team.division_id %> However, I’m not sure what to do to get the division name.

I tried adding the division to each team:

@teams.each do |team|
    team.division = Division.find(team.division_id)
end

But this failed, saying: undefined method 'division=' for #<Team:0x3cf2df0>. I’m not even sure if this is the right way to go about doing this, or if I’m over-complicating things.

  • 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-17T18:51:52+00:00Added an answer on June 17, 2026 at 6:51 pm

    Since you have the relation, you should be able to just do

    team.division.name
    

    Also, you have a typo in your model (you would have never missed this kind of error in a statically typed language like Java. In Ruby you have to write tests, lots of tests)

    belongs_to :divison
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if I'm doing this right: /(https?:\/\/\S+)(\s|(& nbsp;))?/g; This should match a URL
Not sure if I can word this right but here we go. I have
Not sure how to ask this, but i'll give it a try: I have
Not sure the best way to phrase this. I have a database of buildings
Not sure if I'm using the right terminology so this question may already be
Not sure what parallel programing means... but I have two thoughts of that Process
Not sure I have the best title for this question. Feel free to modify
Not sure if this is possible in Python, but I'd like to have an
Not sure if this is the right stack exchange to ask this, but here
Not sure if this belongs here or on Serverfault. I have a Sharepoint installation

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.