Browse files

Ensure all join table attributes will be in the same timestamp.

  • Loading branch information...
1 parent 8bb3b63 commit f3e42292a5792f77681a3fbf03bc8cfc75aeb887 @josevalim josevalim committed Jul 21, 2010
Showing with 21 additions and 11 deletions.
  1. +21 −11 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
View
32 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -45,21 +45,23 @@ def insert_record(record, force = true, validate = true)
if @reflection.options[:insert_sql]
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
else
- relation = Arel::Table.new(@reflection.options[:join_table])
+ relation = Arel::Table.new(@reflection.options[:join_table])
+ timestamps = record_timestamp_columns(record)
+ timezone = record.send(:current_time_from_proper_timezone) if timestamps.any?
+
attributes = columns.inject({}) do |attrs, column|
- case column.name.to_s
+ name = column.name
+ case name.to_s
when @reflection.primary_key_name.to_s
- attrs[relation[column.name]] = @owner.id
+ attrs[relation[name]] = @owner.id
when @reflection.association_foreign_key.to_s
- attrs[relation[column.name]] = record.id
+ attrs[relation[name]] = record.id
+ when *timestamps
+ attrs[relation[name]] = timezone
else
- if record.send(:all_timestamp_attributes).include?(column.name.to_sym)
- if record.record_timestamps
- attrs[relation[column.name]] = record.send(:current_time_from_proper_timezone)
- end
- elsif record.has_attribute?(column.name)
- value = @owner.send(:quote_value, record[column.name], column)
- attrs[relation[column.name]] = value unless value.nil?
+ if record.has_attribute?(name)
+ value = @owner.send(:quote_value, record[name], column)
+ attrs[relation[name]] = value unless value.nil?
end
end
attrs
@@ -121,6 +123,14 @@ def create_record(attributes, &block)
build_record(attributes, &block)
end
end
+
+ def record_timestamp_columns(record)
+ if record.record_timestamps
+ record.send(:all_timestamp_attributes).map(&:to_s)
+ else
+ []
+ end
+ end
end
end
end

0 comments on commit f3e4229

Please sign in to comment.