diff options
author | José Valim <jose.valim@gmail.com> | 2010-07-21 14:50:04 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-21 14:51:15 +0200 |
commit | f3e42292a5792f77681a3fbf03bc8cfc75aeb887 (patch) | |
tree | 169bd06a78bf49d715b11d20a9f94ae93d20ec6d | |
parent | 8bb3b634c07a327b9f1c7e21b4d1fcc407737a9f (diff) | |
download | rails-f3e42292a5792f77681a3fbf03bc8cfc75aeb887.tar.gz rails-f3e42292a5792f77681a3fbf03bc8cfc75aeb887.tar.bz2 rails-f3e42292a5792f77681a3fbf03bc8cfc75aeb887.zip |
Ensure all join table attributes will be in the same timestamp.
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 177d7905c7..e61af93d1e 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -45,21 +45,23 @@ module ActiveRecord 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 @@ module ActiveRecord 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 |