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 6807865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:06+00:00 2026-05-26T19:53:06+00:00

I am wondering how to do the association in Rails correct. First I create

  • 0

I am wondering how to do the association in Rails correct. First I create a City model and an Organisation. Now I want to have an Organisation have a City… this is done by adding the has_many and has_one associations. After that I run rake db:migrate. But somehow it does not create a field city or city_id in my database model. Do I have to do this myself? Shouldn’t rails now create a foreign key constraint in the database?

To see if it has worked I am using rails c and type in Organisation
the answer is the following:

=> Organisation(id: integer, name: string, description: string, url: string, created_at: datetime, updated_at: datetime) 

Please excuse my stupid question… I am a beginner in Rails and everything is still very unfamiliar.

Thanks!


City:

class City < ActiveRecord::Base
  has_many :organisations
end

Organisation:

class Organisation < ActiveRecord::Base
  has_one :city
end

Create City:

class CreateCities < ActiveRecord::Migration
  def change
    create_table :cities do |t|
      t.string :name
      t.string :country

      t.timestamps
    end
  end
end

Create Organisation:

class CreateOrganisations < ActiveRecord::Migration
  def change
    create_table :organisations do |t|
      t.string :name
      t.string :description
      t.string :url

      t.timestamps
    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-05-26T19:53:06+00:00Added an answer on May 26, 2026 at 7:53 pm

    There are a couple things wrong with this.

    1. You need to specify a belongs_to on the other side of a has_many or has_one association. The model that defines a belongs_to association is where the foreign key belongs.

      So if an Organization has_one :city, then a City needs to belongs_to :organization. Alternatively, if a City has_one :organization, then the Organization needs to belongs_to :city.

      Looking at your setup, it looks like you want the belongs_to definition inside the City model.

    2. The migrations aren’t built off the model definitions. Instead, they are built from the db/migrations folder. A migration is created when you run the rails g model command (or rails g migration). In order to get a foreign key, you need to tell the generator to create it.

      rails generate model organization name:string description:string url:string city_id:integer
      

      Or

      rails generate model city name:string description:string url:string organization_id:integer
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering, how an association like the following might be done in Rails:
Wondering if this is possible. We have an 3rd Party library that contains identification
I was just wondering if it's possible to rename an association in Rails. Let's
I have a 'Complex' model which hasMany Unit. My model associations are correct, everything
I have a model with a has_many association. The association also has an :after_add
I am relatively new to Rails. I have a User model through Devise. I
I have a simple AR association like this: Question has_many :answers Answer belongs_to :question
Wondering how to open many new windows with Javascript. I have found plenty of
Wondering if anyone can help me with this annoying but trivial (in terms of
Hey, I'm very new to Ruby and Rails. I was wondering if I was

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.