What is the right way to override a setter method in Ruby on Rails?

前端 未结 5 832
春和景丽
春和景丽 2020-11-28 18:05

I am using Ruby on Rails 3.2.2 and I would like to know if the following is a \"proper\"/\"correct\"/\"sure\" way to override a setter method for a my class attribute.

相关标签:
5条回答
  • 2020-11-28 18:53

    Use the super keyword:

    def attribute_name=(value)
      super(value.some_custom_encode)
    end
    

    Conversely, to override the reader:

    def attribute_name
      super.some_custom_decode
    end
    
    0 讨论(0)
  • 2020-11-28 18:59

    In rails 4

    let say you have age attribute in your table

    def age=(dob)   
        now = Time.now.utc.to_date
        age = now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
        super(age) #must add this otherwise you need to add this thing and place the value which you want to save. 
      end
    

    Note: For new comers in rails 4 you don't need to specify attr_accessible in model. Instead you have to white-list your attributes at controller level using permit method.

    0 讨论(0)
  • 2020-11-28 19:01

    =========================================================================== Update: July 19, 2017

    Now the Rails documentation is also suggesting to use super like this:

    class Model < ActiveRecord::Base
    
      def attribute_name=(value)
        # custom actions
        ###
        super(value)
      end
    
    end
    

    ===========================================================================

    Original Answer

    If you want to override the setter methods for columns of a table while accessing through models, this is the way to do it.

    class Model < ActiveRecord::Base
      attr_accessible :attribute_name
    
      def attribute_name=(value)
        # custom actions
        ###
        write_attribute(:attribute_name, value)
        # this is same as self[:attribute_name] = value
      end
    
    end
    

    See Overriding default accessors in the Rails documentation.

    So, your first method is the correct way to override column setters in Models of Ruby on Rails. These accessors are already provided by Rails to access the columns of the table as attributes of the model. This is what we call ActiveRecord ORM mapping.

    Also keep in mind that the attr_accessible at the top of the model has nothing to do with accessors. It has a completely different functionlity (see this question)

    But in pure Ruby, if you have defined accessors for a class and want to override the setter, you have to make use of instance variable like this:

    class Person
      attr_accessor :name
    end
    
    class NewPerson < Person
      def name=(value)
        # do something
        @name = value
      end
    end
    

    This will be easier to understand once you know what attr_accessor does. The code attr_accessor :name is equivalent to these two methods (getter and setter)

    def name # getter
      @name
    end
    
    def name=(value) #  setter
      @name = value
    end
    

    Also your second method fails because it will cause an infinite loop as you are calling the same method attribute_name= inside that method.

    0 讨论(0)
  • 2020-11-28 19:03

    I have found that (at least for ActiveRecord relationship collections) the following pattern works:

    has_many :specialties
    
    def specialty_ids=(values)
      super values.uniq.first(3)
    end
    

    (This grabs the first 3 non-duplicate entries in the array passed.)

    0 讨论(0)
  • 2020-11-28 19:03

    Using attr_writer to overwrite setter attr_writer :attribute_name

      def attribute_name=(value)
        # manipulate value
        # then send result to the default setter
        super(result)
      end
    
    0 讨论(0)
提交回复
热议问题