From f1af967e0055a33c45071848a049ff342e9c291e Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Sun, 12 Apr 2015 20:20:48 -0700 Subject: Add note wrt foreign key constraint to ensure data integrity Thanks for patch @sgrif :trollface: Closes #18216 --- guides/source/association_basics.md | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'guides/source') diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 113cfcc274..498bc449d2 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -146,6 +146,16 @@ class CreateSuppliers < ActiveRecord::Migration end ``` +If you happen to want to create a unique index with foreign key constraint... +Then I would suggest using this for creating the accounts table instead: + +```ruby + create_table :accounts do |t| + t.belongs_to :supplier, index: true, unique: true, foreign_key: true + # ... + end +``` + ### The `has_many` Association A `has_many` association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a `belongs_to` association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this: -- cgit v1.2.3