aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
authorJon Moss <maclover7@users.noreply.github.com>2016-01-18 09:33:03 -0500
committerJon Moss <maclover7@users.noreply.github.com>2016-01-18 09:33:03 -0500
commit721e45c90c79378fa854de4d16a3f154d1447d59 (patch)
treead8b35fd53072613796b754ac57393833c3b0cb9 /guides/source
parentd95d680d68fd833635e1f7bc46e1df0a3535c4de (diff)
parentf72fe2d5a65002195c15a0cfe3ae3c51db825d36 (diff)
downloadrails-721e45c90c79378fa854de4d16a3f154d1447d59.tar.gz
rails-721e45c90c79378fa854de4d16a3f154d1447d59.tar.bz2
rails-721e45c90c79378fa854de4d16a3f154d1447d59.zip
Merge pull request #22394 from j-dexx/update_association_basics_guide
Updated the guides for having a distinct has_many through at the data…
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/association_basics.md16
1 files changed, 13 insertions, 3 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index d83dda7228..520b324ea7 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -1872,11 +1872,21 @@ If you want to make sure that, upon insertion, all of the records in the
persisted association are distinct (so that you can be sure that when you
inspect the association that you will never find duplicate records), you should
add a unique index on the table itself. For example, if you have a table named
-`person_articles` and you want to make sure all the articles are unique, you could
-add the following in a migration:
+`readings` and you want to make sure the articles can only be added to a person once,
+you could add the following in a migration:
```ruby
-add_index :person_articles, :article, unique: true
+add_index :readings, [:person_id, :article_id], unique: true
+```
+
+Once you have this unique index, attempting to add the article to a person twice
+will raise an `ActiveRecord::RecordNotUnique` error:
+
+```ruby
+person = Person.create(name: 'Honda')
+article = Article.create(name: 'a1')
+person.articles << article
+person.articles << article # => ActiveRecord::RecordNotUnique
```
Note that checking for uniqueness using something like `include?` is subject