23 Oct '13, 2pm

#Rails protip, use :uniq to remove duplicates as "has_many :contributors,through: :contributions,uniq: true" (2006!)

If you don't follow the Rails Trac commit log , you may find this interesting. Jeremy Kemper just checked in a change to enable the :uniq option for has_many :through associations. So what does the :uniq option do? The docs for the has_many options say: :uniq - if set to true, duplicates will be omitted from the collection. Useful in conjunction with :through. This is very handy when there are multiple connections between two model objects via a join model but you only care about whether there are any connections at all. Let's go back to my favorite example, where contributors contribute to books in various roles, such as author, editor, illustrator, etc. I'll omit the migrations for Book and Contributor as they aren't interesting for what we're doing here. create_table "contributions" do |t| t.column "book_id", :integer t.column "contributor_id", :integer t.column "role",...

Full article: http://blog.hasmanythrough.com/2006/5/6/through-gets-uniq

Tweets