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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:40:04+00:00 2026-05-21T07:40:04+00:00

Just starting to learn Ruby on Rails. I’m using RoR 3. I have read

  • 0

Just starting to learn Ruby on Rails. I’m using RoR 3. I have read this: http://guides.rubyonrails.org/association_basics.html

But I want to make sure I understand completely.

When creating a new model (I’m doing via scaffold for now), should I specify foreign_key fields at that point, or does the association handle that completely? I believe that association is only at app level, not at the db level, correct?

So I think I must do:

rails generate scaffold post body:text title:string user_id:integer

So in summary, when creating a blog application, must I specify the user_id field in the post model, or does the user model’s has_many :posts take care of actually adding that to the db (mine is mysql) when I migrate?

And if the answer is that I should do them when I create the model in the first place (via scaffold or by hand), what happens when I decide later on that I want to add a foreign key, must I add that as an execute statement in a new migration?

  • 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-21T07:40:04+00:00Added an answer on May 21, 2026 at 7:40 am

    You’re correct. You need to specify the foreign key when you create your scaffold/model/migration as you stated to get the DB to be correct, and the has_many takes cares of the model for you.

    So for initial generation of a scaffold (or model), just do:

    rails generate scaffold post body:text title:string user_id:integer
    

    as you stated, and add the has_many for the model itself.

    For additions later on, you would make up a new migration, something like (assuming you want to use generation, but you could write your own migration):

    rails generate migration add_user_id_to_posts user_id:integer
    

    With that, you can run a rake db:migrate, and then update your model with a has_many or whatever association you need.

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

Sidebar

Related Questions

I am just starting to learn Ruby on Rails and have a newbie question
I'm just starting to learn Ruby and have a problem with encoding; require 'rubygems'
im just starting to learn about sockets and i have been given this code,
I am just starting to learn Ruby (to eventually move to RoR), but I
I am just starting to learn javascript, so I don't have the skills to
I'm just starting to learn C++ so you'll have to bear with my ignorance.
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
I'm just starting to learn the C programming language. I've written this simple piece
I am just starting to learn Python, but I have already run into some
I am just starting to learn ruby. It seems that the default gems install

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.