From de12a6a0efa51ab9d47bad0c94486cdbd1da900b Mon Sep 17 00:00:00 2001 From: wangjohn Date: Tue, 2 Apr 2013 13:36:25 -0400 Subject: Changed the guides to use distinct instead of uniq for associations (see commit a1bb6c8b which changes the naming). I've also provided an example of how distinctness when inserting into an association can be achieved. --- guides/source/association_basics.md | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'guides/source/association_basics.md') diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 18d9f5c562..8d203d265a 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -1648,9 +1648,10 @@ The `select` method lets you override the SQL `SELECT` clause that is used to re WARNING: If you specify your own `select`, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error. -##### `uniq` +##### `distinct` -Use the `uniq` method to keep the collection free of duplicates. This is mostly useful together with the `:through` option. +Use the `distinct` method to keep the collection free of duplicates. This is +mostly useful together with the `:through` option. ```ruby class Person < ActiveRecord::Base @@ -1666,14 +1667,15 @@ person.posts.inspect # => [#, #] Reading.all.inspect # => [#, #] ``` -In the above case there are two readings and `person.posts` brings out both of them even though these records are pointing to the same post. +In the above case there are two readings and `person.posts` brings out both of +them even though these records are pointing to the same post. -Now let's set `uniq`: +Now let's set `distinct`: ```ruby class Person has_many :readings - has_many :posts, -> { uniq }, through: :readings + has_many :posts, -> { distinct }, through: :readings end person = Person.create(name: 'Honda') @@ -1684,7 +1686,29 @@ person.posts.inspect # => [#] Reading.all.inspect # => [#, #] ``` -In the above case there are still two readings. However `person.posts` shows only one post because the collection loads only unique records. +In the above case there are still two readings. However `person.posts` shows +only one post because the collection loads only unique records. + +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_posts`` and you want to make sure all the posts are unique, you could +add the following in a migration: + +```ruby +add_index :person_posts, :post, :unique => true +``` + +Note that checking for uniqueness using something like ``include?`` is subject +to race conditions. Do not attempt to use ``include?`` to enforce distinctness +in an association. For instance, using the post example from above, the +following code would be racy because multiple users could be attempting this +at the same time: + +```ruby +person.posts << post unless person.posts.include?(post) +``` #### When are Objects Saved? -- cgit v1.2.3