<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -15,6 +15,10 @@ module CommonThread
           unless_condition = blank_fields.map{|field| &quot;(assoc_attr['#{field}'].blank? and assoc_attr[:#{field}].blank?)&quot;}.join ' or '
         end
         
+        if options[:keep_unique_by]
+          @keep_unique_by = options[:keep_unique_by].to_sym
+        end
+        
         class_eval %{
           def update_#{association_name}_attributes
             self.#{association_name}_attributes = @#{association_name}_attributes if @#{association_name}_attributes
@@ -24,8 +28,17 @@ module CommonThread
             if self.new_record?
               @#{association_name}_attributes = attributes
             else
+              keep_unique_by = #{@keep_unique_by ? &quot;:&quot;+@keep_unique_by.to_s : &quot;nil&quot;}
               for assoc_attr in attributes
-                #{association_name} = assoc_attr['id'].blank? ? self.#{association_name}.new : self.#{association_name}.find(assoc_attr['id'])
+                assoc_attr = HashWithIndifferentAccess.new(assoc_attr)
+                if not assoc_attr[:id].blank?
+                  #{association_name} = self.#{association_name}.find_by_id(assoc_attr[:id]) || self.#{association_name}.new
+                elsif keep_unique_by and !assoc_attr[keep_unique_by].blank?
+                  #{association_name} = self.#{association_name}.find_by_#{@keep_unique_by}(assoc_attr[keep_unique_by]) || self.#{association_name}.new
+                else
+                  #{association_name} = self.#{association_name}.new
+                end
+                
                 #{association_name}.update_attributes(assoc_attr) unless #{unless_condition}
               end
             end</diff>
      <filename>lib/association_attributes.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>8cb4874a5e089817adb0cc78a137f3024dc5f54d</id>
    </parent>
  </parents>
  <author>
    <name>bwyrosdick</name>
    <email>ben.wyrosdick@gmail.com</email>
  </author>
  <url>http://github.com/bwyrosdick/association-attributes/commit/b8579afc9658b4e5a34dfc6c05f60c6c277a8239</url>
  <id>b8579afc9658b4e5a34dfc6c05f60c6c277a8239</id>
  <committed-date>2008-11-03T13:51:08-08:00</committed-date>
  <authored-date>2008-11-03T13:51:08-08:00</authored-date>
  <message>added unique_by check</message>
  <tree>27d29b993e3e29da30752d05a337b8bd3d56bfc6</tree>
  <committer>
    <name>bwyrosdick</name>
    <email>ben.wyrosdick@gmail.com</email>
  </committer>
</commit>
