-
Notifications
You must be signed in to change notification settings - Fork 19
Add Rails 5 support #21
base: master
Are you sure you want to change the base?
Conversation
|
Hi @Kenneth-KT. Our general stance has been that this library will not need to support Rails 5, due to changes being made in Rails core that have similar goals. See #13 (comment). Is there something that this library does that's missing in the Rails 5 solution, which would make it beneficial for us to support Rails 5? |
@mtuckergd Yes, this library does have features that ActiveRecord 5.0 does not provide.
Consider the following code sample: person = Person.create!
pets = 100.times.collect { Pet.create!(person: person) }
pets.each_slice(10) do |part_of_pets|
ActiveRecord::Base.transaction do
part_of_pets.each { |x| x.touch_later }
end
endThis code sample generates 110 queries: You can see that the same If we wrap the code with person = Person.create!
pets = 100.times.collect { Pet.create!(person: person) }
ActiveRecord::Base.delay_touching do
pets.each_slice(10) do |part_of_pets|
ActiveRecord::Base.transaction do
part_of_pets.each { |x| x.touch_later }
end
end
endIt only generates 2 queries: We are running a production system that needs to support bulk importing and updating 100k+ record on daily basis, all those queries saved dramatically improved performance of our system. |
| super | ||
| if ActiveRecord::VERSION::MAJOR >= 5 | ||
| def touch(*names, time: nil) | ||
| names = self.class.send(:timestamp_attributes_for_update_in_model) if names.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @Kenneth-KT, these changes failed for us locally on Rails v5.0.7 with the following error:
NoMethodError:
undefined method `timestamp_attributes_for_update_in_model' for #<Class:0x00007fbcde0ace48>
(To clarify, we're in the process of upgrading an application that currently uses DelayTouching from Rails 4.2 to 5.0.)
From the source, it looks like even through v5.2.2, this method is accessed on the instance, not class, level:
https://github.com/rails/rails/blob/v5.0.7/activerecord/lib/active_record/touch_later.rb#L18
https://github.com/rails/rails/blob/v5.2.2/activerecord/lib/active_record/touch_later.rb#L20
We're now using the following instead:
self.send(:timestamp_attributes_for_update_in_model) if names.empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I'm admittedly not familiar with the Rails 5 persistence implementation, but what's the need for timestamp_attributes_for_update_in_model here?
It seems like a nil value is expected in the case of only touching defaults, and is handled here:
columns << nil if columns.empty? #if no arguments are passed, we will use nil to infer default column attributes = records.first.send(:timestamp_attributes_for_update_in_model)
This PR adds Rails 5.0 support to the
activerecord-delay_touchinggem.This PR does not break Rails 4.0 compatibility.
Rspec has passed against both latest Rails version 5.2.1 and 4.2.9.
One drawback of this implementation is it does not respect
timeinxxx.touch(time: time), it will only be filled with current time.