Activerecord 在AR中保存新属性之前,如何获取属性的当前值?

Activerecord 在AR中保存新属性之前,如何获取属性的当前值?,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我有一个具有以下属性的模型: 一些数字 一些字符串 我想根据some\u number的当前值和更新时some\u number的新值,在some\u string中添加一些值 例如: 当前的某些编号为4 某些编号的新(更新)值为5 我想在覆盖一些数字之前,将4+5字符串放入一些字符串中 如何使其工作?很难从您的问题中看出,但是假设您想要某些字符串的结尾处的某个数字的当前值 class Thing < ActiveRecord::Base def initiaize @numb

我有一个具有以下属性的模型:

  • 一些数字
  • 一些字符串
  • 我想根据
    some\u number
    的当前值和更新时
    some\u number
    的新值,在
    some\u string
    中添加一些值

    例如:

    当前的
    某些编号
    4

    某些编号的新(更新)值为5

    我想在覆盖
    一些数字之前,将
    4+5
    字符串放入
    一些字符串中


    如何使其工作?

    很难从您的问题中看出,但是假设您想要
    某些字符串的
    结尾处的某个数字的当前值

    class Thing < ActiveRecord::Base
      def initiaize
        @numbers = []
        super
      end
    
      def some_number=(value)
        @numbers << value
        write_attribute :some_string, @numbers.join('+')
        super
      end
    end
    
    class Thing@数字很难从你的问题中分辨出来,但是假设你想在
    一些字符串
    的末尾使用
    一些数字
    的当前值

    class Thing < ActiveRecord::Base
      def initiaize
        @numbers = []
        super
      end
    
      def some_number=(value)
        @numbers << value
        write_attribute :some_string, @numbers.join('+')
        super
      end
    end
    
    class Thing@数字很难从你的问题中分辨出来,但是假设你想在
    一些字符串
    的末尾使用
    一些数字
    的当前值

    class Thing < ActiveRecord::Base
      def initiaize
        @numbers = []
        super
      end
    
      def some_number=(value)
        @numbers << value
        write_attribute :some_string, @numbers.join('+')
        super
      end
    end
    
    class Thing@数字很难从你的问题中分辨出来,但是假设你想在
    一些字符串
    的末尾使用
    一些数字
    的当前值

    class Thing < ActiveRecord::Base
      def initiaize
        @numbers = []
        super
      end
    
      def some_number=(value)
        @numbers << value
        write_attribute :some_string, @numbers.join('+')
        super
      end
    end
    
    class Thing@数字如果我需要根据更新事件上的其他属性值修改某些属性,我会这样做:

    class Thing < ActiveRecord::Base
        before_update :modify_some_string
        protected
        def modify_some_string
                # some actions with self.some_string and self.some_number
                # and self.some_string_was and self.some_number_was
        end
    end
    
    class Thing
    如果我需要根据更新事件中的其他属性值修改某些属性,我会这样做:

    class Thing < ActiveRecord::Base
        before_update :modify_some_string
        protected
        def modify_some_string
                # some actions with self.some_string and self.some_number
                # and self.some_string_was and self.some_number_was
        end
    end
    
    class Thing
    如果我需要根据更新事件中的其他属性值修改某些属性,我会这样做:

    class Thing < ActiveRecord::Base
        before_update :modify_some_string
        protected
        def modify_some_string
                # some actions with self.some_string and self.some_number
                # and self.some_string_was and self.some_number_was
        end
    end
    
    class Thing
    如果我需要根据更新事件中的其他属性值修改某些属性,我会这样做:

    class Thing < ActiveRecord::Base
        before_update :modify_some_string
        protected
        def modify_some_string
                # some actions with self.some_string and self.some_number
                # and self.some_string_was and self.some_number_was
        end
    end
    
    class Thing
    Override the
    some_number=
    setter--cf您能更清楚地解释一下如何通过重写setter来解决问题吗?如何在该setter中获取当前值和新值?重写
    some\u number=
    setter--cf您能更清楚地解释一下如何通过重写setter来解决问题吗?如何在该setter中获取当前值和新值?重写
    some\u number=
    setter--cf您能更清楚地解释一下如何通过重写setter来解决问题吗?如何在该setter中获取当前值和新值?重写
    some\u number=
    setter--cf您能更清楚地解释一下如何通过重写setter来解决问题吗?如何获取该setter中的当前值和新值?请注意,此解决方案适用于对
    某些\u编号
    的每次更改,与模型是否保存到数据库无关。请注意,此解决方案适用于对
    某些\u编号
    的每次更改,与模型是否保存到数据库无关。请注意,此解决方案适用于对某个编号的每次更改,与模型是否保存到数据库无关。请注意,此解决方案适用于对某个编号的每次更改,与模型是否保存到数据库无关。