Is there a way to get a source class inside after_touch callback?
My situation is that I must not process after_touch callback if its source class is BoardItemPosition, but for remaining 8 classes that also use belongs_to :item, touch: true relationship I must process callback as usual.
I can not simply delete touch: true from BoardItemPosition class because this touch is also used by Audited gem to watch and store object versions.
class BoardItemPosition < ActiveRecord::Base
belongs_to :item, touch: true
end
class Comment < ActiveRecord::Base
belongs_to :item, touch: true
end
# ... 8 more classes that belongs to Item
class Item < ActiveRecord::Base
after_touch :rtn_item_updated
def rtn_item_updated
return if self.touch_class == BoardItemPosition # <--- How to check a class?
# unrelated method logic below...
end
end
What you are asking may not be possible. As shown in Rails guides, The only parameter touch callback accepts is the entity itself, you will have to use below form of callback implementation
after_touch do |record|
puts "#{record.class}"
end
#=> Item
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With