Skip to content

Commit

Permalink
revises recent commit related to :uniq => true
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Jul 9, 2010
1 parent 44b752b commit 86d5c72
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -889,7 +889,7 @@ module ClassMethods
# Specifies type of the source association used by <tt>has_many :through</tt> queries where the source # Specifies type of the source association used by <tt>has_many :through</tt> queries where the source
# association is a polymorphic +belongs_to+. # association is a polymorphic +belongs_to+.
# [:uniq] # [:uniq]
# If true, duplicates will be omitted from the collection. Works only in conjunction with <tt>:through</tt>. # If true, duplicates will be omitted from the collection. Useful in conjunction with <tt>:through</tt>.
# [:readonly] # [:readonly]
# If true, all the associated objects are readonly through the association. # If true, all the associated objects are readonly through the association.
# [:validate] # [:validate]
Expand Down
50 changes: 22 additions & 28 deletions railties/guides/source/association_basics.textile
Expand Up @@ -1371,47 +1371,41 @@ The +:through+ option specifies a join model through which to perform the query.


h6(#has_many-uniq). +:uniq+ h6(#has_many-uniq). +:uniq+


Specify the +:uniq => true+ option to remove duplicates from the collection. It only works with +:through+ option. Set the +:uniq+ option to true to keep the collection free of duplicates. This is mostly useful together with the +:through+ option.


<ruby> <ruby>
class Person < ActiveRecord::Base class Person < ActiveRecord::Base
has_many :readers has_many :readings
has_many :posts, :through => :readers has_many :posts, :through => :readings

def self.lab
person = Person.create(:name => 'john')
p = Post.create(:name => 'a1')
person.posts << p
person.posts << p
person.reload
puts person.posts.inspect #=> [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
puts Reader.all.inspect #=> [#<Reader id: 12, person_id: 5, post_id: 5>, #<Reader id: 13, person_id: 5, post_id: 5>]
end
end end

person = Person.create(:name => 'john')
post = Post.create(:name => 'a1')
person.posts << post
person.posts << post
person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
</ruby> </ruby>


In the above case +readers+ table has two records and +person.posts+ brings out both of these records even though these records are basically pointing to the same +post+ record. 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 add +uniq => true+ option. Now let's set +:uniq+ to true:


<ruby> <ruby>
class Person class Person
has_many :readers has_many :readings
has_many :posts, :through => :readers, :uniq => true has_many :posts, :through => :readings, :uniq => true

def self.experiment
person = Person.create(:name => 'honda')
p = Post.create(:name => 'a1')
person.posts << p
person.posts << p
person.reload
puts person.posts.inspect #=> [#<Post id: 7, name: "a1">]
puts Reader.all.inspect #=> [#<Reader id: 16, person_id: 7, post_id: 7>, #<Reader id: 17, person_id: 7, post_id: 7>]
end
end end

person = Person.create(:name => 'honda')
post = Post.create(:name => 'a1')
person.posts << post
person.posts << post
person.posts.inspect # => [#<Post id: 7, name: "a1">]
Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
</ruby> </ruby>


In the above case +readers+ table still has two records. However +person.posts+ will show only one +post+ record because collection will load only +unique+ records. In the above case there are still two readings. However +person.posts+ shows only one post because collection loads only unique records.


h6(#has_many-validate). +:validate+ h6(#has_many-validate). +:validate+


Expand Down

0 comments on commit 86d5c72

Please sign in to comment.