diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-07-20 23:52:24 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-21 14:45:36 +0200 |
commit | 8bb3b634c07a327b9f1c7e21b4d1fcc407737a9f (patch) | |
tree | c61aef76976551885dc004aa2ba4d22e5146b403 /activerecord/lib | |
parent | 01add55d6a6a67fb487afa040998575111511b6d (diff) | |
download | rails-8bb3b634c07a327b9f1c7e21b4d1fcc407737a9f.tar.gz rails-8bb3b634c07a327b9f1c7e21b4d1fcc407737a9f.tar.bz2 rails-8bb3b634c07a327b9f1c7e21b4d1fcc407737a9f.zip |
Timestamp columns of HABTM join table should record timestamps
[#5161 state:resolved]
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/timestamp.rb | 19 |
2 files changed, 21 insertions, 4 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 9ec63e3fca..177d7905c7 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 @@ -53,7 +53,11 @@ module ActiveRecord when @reflection.association_foreign_key.to_s attrs[relation[column.name]] = record.id else - if record.has_attribute?(column.name) + 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? end diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index 341cc87be5..6c1e376745 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -39,8 +39,9 @@ module ActiveRecord if record_timestamps current_time = current_time_from_proper_timezone - write_attribute('created_at', current_time) if respond_to?(:created_at) && created_at.nil? - write_attribute('created_on', current_time) if respond_to?(:created_on) && created_on.nil? + timestamp_attributes_for_create.each do |column| + write_attribute(column.to_s, current_time) if respond_to?(column) && self.send(column).nil? + end timestamp_attributes_for_update_in_model.each do |column| write_attribute(column.to_s, current_time) if self.send(column).nil? @@ -65,7 +66,19 @@ module ActiveRecord end def timestamp_attributes_for_update_in_model #:nodoc: - [:updated_at, :updated_on].select { |elem| respond_to?(elem) } + timestamp_attributes_for_update.select { |elem| respond_to?(elem) } + end + + def timestamp_attributes_for_update #:nodoc: + [:updated_at, :updated_on] + end + + def timestamp_attributes_for_create #:nodoc: + [:created_at, :created_on] + end + + def all_timestamp_attributes #:nodoc: + timestamp_attributes_for_update + timestamp_attributes_for_create end def current_time_from_proper_timezone #:nodoc: |