Specifying a column name in link migration

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

 add_column :post, :user, :references 

Creates a column named user_id in the posts table. But what if, instead of user_id I want something like author_id ? How can i do this?

+107
ruby-on-rails activerecord ruby-on-rails-3 rails-migrations
Dec 04 '12 at 1:20
source share
6 answers

Do it manually:

 add_column :post, :author_id, :integer 

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

 def post belongs_to :user, :foreign_key => 'author_id' end 
+52
Dec 04 '12 at 1:28
source share

In Rails 4. 2+, you can also set foreign keys in the database, which is a great idea .

For simple associations, this can also be done using t.references adding foreign_key: true , but in this case you will need two lines.

 # The migration add_reference :posts, :author, index: true add_foreign_key :posts, :users, column: :author_id # The model belongs_to :author, class_name: "User" 
+228
Apr 11 '15 at 12:27
source share

For Rails 5+

Initial Definition:

If you define a table of Post models, you can set references , index and foreign_key on the same line:

 t.references :author, index: true, foreign_key: { to_table: :users } 



Update existing:

If you add links to an existing table, you can do this:

 add_reference :posts, :author, foreign_key: { to_table: :users } 



Note: The default value for index is true.

+182
Feb 05 '17 at 19:11
source share

In rails 4, when using postgresql and gem schema_plus you can simply write

 add_reference :posts, :author, references: :users 

This will create an author_id column that correctly references users(id) .

And in your model you write

 belongs_to :author, class_name: "User" 

Please note that when creating a new table, you can write it as follows:

 create_table :things do |t| t.belongs_to :author, references: :users end 

Note: the schema_plus gem in its entirety is not compatible with rails 5+, but this functionality is provided by the gem schema_auto_foreign_keys (part of schema_plus), which is compatible with rails 5.

+80
Mar 13 '14 at 16:04
source share

If you are not using a foreign key, it does not matter what the actual table name of another table is.

 add_reference :posts, :author 

According to Rails 5 , if you use a foreign key, you can specify the name of another table in the foreign key parameters. (see https://github.com/rails/rails/issues/21563 for a discussion)

 add_reference :posts, :author, foreign_key: {to_table: :users} 

Prior to Rails 5, you should add the foreign key as a separate step:

 add_foreign_key :posts, :users, column: :author_id 
+47
Nov 08 '15 at 6:14
source share

alias_attribute (new_name, old_name) is very convenient. Just create your model and relationship:

 rails g model Post title user:references 

then edit the model and add the attribute alias with

 alias_attribute :author, :user 

After that, you will be able to run things like

 Post.new(title: 'My beautiful story', author: User.first) 
-2
Mar 30 '17 at 16:11
source share



All Articles