1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-08 01:28:02 +00:00

Add Create A Custom Named References Column as a Rails til

This commit is contained in:
jbranchaud
2021-03-03 21:01:23 -06:00
parent a3cf90c826
commit e5a50ad12a
2 changed files with 49 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
# Create A Custom Named References Column
The `t.references` and `add_reference` methods both create a foreign key column
name based on the target table.
For instance,
```ruby
add_reference :guests, :user, foreign_key: true, index: true, null: false
```
would create a `user_id` column on the `guests` table.
At times, you'll need to customize the name of the foreign key column. The
Rails migration DSL supports this with the `foreign_key` option.
Here is an example that shows the syntax for both a new table and an existing
table.
```ruby
class AddInvitedByColumnToUser < ActiveRecord::Migration[6.1]
def change
create_table :guests, id: :uuid do |t|
t.string :email, null: false
t.timestamps
t.references :invited_by,
type: :uuid,
index: true,
null: false,
foreign_key: { to_table: :users }
end
add_reference :guests, :signed_up_as,
type: :uuid,
index: true,
null: false,
foreign_key: { to_table: :users }
end
end
```
The `t.references` call creates a foreign key column to the `users` table named
`invited_by`. The `add_reference` call adds a `signed_up_as` foreign key column
to `guests` that points to users.
[source](https://stackoverflow.com/a/42056089/535590)