在 Rails 中使用 attr_accessor [英] usage of attr_accessor in Rails

查看:54
本文介绍了在 Rails 中使用 attr_accessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你什么时候在 Rails 模型中使用 attr_reader/attr_writer/attr_accessor?

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

推荐答案

attr_accessor 可用于您不想直接存储在数据库中并且仅在对象的生命周期内存在的值(例如密码).

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 可以用作执行此类操作的几种替代方法之一:

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

def instance_value
  "my value"
end

这篇关于在 Rails 中使用 attr_accessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆