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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:14:53+00:00 2026-05-17T17:14:53+00:00

I have the following Rails migration which works perfectly (irrelevant pieces removed): create_table :comments

  • 0

I have the following Rails migration which works perfectly (irrelevant pieces removed):

create_table :comments do |t|
  t.text :body
  t.references :post
end

Now I’d like to add an author column to my comments table (which is the userid of a user), but I have no idea how to do it (I’m tempted to just write the MySql-specific syntax using an execute).

I’ve been looking at add_column here which doesn’t mention references. I’ve actually found TableDefinition#references but I have no idea how to use it with an add_column statement.

Is this possible? Also, is it true that, for MySql, the “references” functionality does not actually establish relationships between the tables?

  • 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-17T17:14:53+00:00Added an answer on May 17, 2026 at 5:14 pm

    While it’s too late to get any points out of this, I thought I’d post the best way for posterity 🙂

    use change_table instead of create_table to add columns to a table that already exists, with all the TableDefinition goodness:

    self.up do
      change_table :comments do |t|
        t.references :author
      end
    end
    

    This might seem trivial, but other gems like Devise make heavy use of their own custom table definitions, and this way you can still use them.

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

Sidebar

Related Questions

I have the following rails migration: create_table :articles do |t| t.integer :user_id, :allow_null =>
I have the following Rails model: class CreateFoo < ActiveRecord::Migration def self.up create_table :foo
I have the following Rails 3 migration which adds a column game_type to a
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
I had a model customer, which I have generated using the following rails g
I have a sequence of migrations in a rails app which includes the following
I have the following rails code in a loop that has a link for
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I am using Rails 3.2. I have following models: Blog Comment User class Blog
In my rails view(index.html.erb), i have following structure <div> <%= render :partial => create

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.