touch_if option?

Something that I believe would be useful to prevent overzealous “updates” via touch would be to allow a lambda to be specified to determine if the touch should occur or not.

class Client

belongs_to :project, touch: -> {|rec| rec.name_changed?}

end

In this case, the project association is only “touched” if only the client name changes. This way if there’s a lot of client data that has no value to the project object and the project is only concerned with a change to just one client attribute (for example) this would only trigger the touch in that case.

Since an attribute can be specified for touch, then perhaps the option should be called touch_if and the touch is always performed when the main object is destroyed.

class Client
belongs_to :project, touch: true, touch_if: -> {|rec| rec.name_changed?}

end

Thoughts?

This is related to this:

https://github.com/rails/rails/pull/12772

In my opinion I prefer to have that kind of explicit feature disable instead some magic lambda condition.

I’m struggling to see how to translate my example into this style.

This is the best I can come up with (and looks horribley messy):

`client.attributes = params[:client]

`

if client.name_changed?

client.save # triggers touch on the associated project

else

` ActiveRecord::Base.no_touching do

`

client.save # no touch triggered

end

end