usage of attr_accessor in Rails

前端 未结 5 652
不思量自难忘°
不思量自难忘° 2020-11-27 05:53

When do you use attr_reader/attr_writer/attr_accessor in Rails models?

5条回答
  •  萌比男神i
    2020-11-27 06:31

    attr_accessor can be used for values you don't want to store in the database directly and that will only exist for the life of the object (e.g. passwords).

    attr_reader can be used as one of several alternatives to doing something like this:

    def instance_value
      "my value"
    end
    

提交回复
热议问题