Search code examples
ruby-on-railsruby-on-rails-3activerecordrails-migrations

Specifying column name in a "references" migration


I want to make a migration in Rails, referencing another table. Usually, I would do something like:

add_column :post, :user, :references

This creates a column named user_id in posts table. But what if, instead of user_id, I want something like author_id? How can I do that?


Solution

  • Do it manually:

    add_column :post, :author_id, :integer
    

    but now, when you create the belongs_to statement, you will have to modify it, so now you have to call

    def post
        belongs_to :user, :foreign_key => 'author_id'
    end