Understanding "attribute_will_change!" method

I want to overwrite getter store_accessor. Which can be found here . The code is here:

# File activerecord/lib/active_record/store.rb, line 74
      def store_accessor(store_attribute, *keys)
        keys = keys.flatten

        _store_accessors_module.module_eval do
          keys.each do |key|
            define_method("#{key}=") do |value|
              write_store_attribute(store_attribute, key, value)
            end

            define_method(key) do
              read_store_attribute(store_attribute, key)
            end
          end
        end

        self.stored_attributes[store_attribute] ||= []
        self.stored_attributes[store_attribute] |= keys
      end

I got the functionality that I was after, however, if I also had to rewrite setter, there is a method that is not clear to me, which is inside the method write_store_attribute(...)(found here ).

The code is here:

# File activerecord/lib/active_record/store.rb, line 108
      def write_store_attribute(store_attribute, key, value)
        attribute = initialize_store_attribute(store_attribute)
        if value != attribute[key]
          send :"#{store_attribute}_will_change!"
          attribute[key] = value
        end
      end

A method that I do not understand "#{whatever_store_att}_will_change!".

If I were to overwrite the installer, I would use update_attributesor update_column. This method makes the assignment attribute[key]=valueactually change the field in the database, what makes it equivalent update_attributes?

+4
1

activemodel, , , db ( ).

, activerecord , ( , ).

, activerecord , , activerecord, (, - , ).

, : - rails ( ), _will_change!, db , .

+8

Source: https://habr.com/ru/post/1542668/


All Articles