什么是datamapper中的attr_accessor-红宝石 [英] what is attr_accessor in datamapper - ruby

查看:49
本文介绍了什么是datamapper中的attr_accessor-红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是datamapper中的newby。我在这个论坛上看到了这段代码。

I am a newby in datamapper. I saw this code in this forum.

    class User
  include DataMapper::Resource

  property :id,                 Serial
  property :email,              String, :required => true, :unique => true, :format  => :email_address,
  property :name,               String
  property :hashed_password,    String
  property :salt,               String  
  property :created_at,         DateTime

  attr_accessor :password, :password_confirmation

属性意味着它将定义数据库表中的字段。attr_accessor的含义是什么..是否是模型中的字段,而不是数据库中的字段?。

property would mean that it defines the field in the database table..what does attr_accessor means..is it kind of field in the model but not in the database..

谢谢

推荐答案

是的,您是对的。它是模型的属性(字段),但不在数据库中。您可以使用这样的属性来保存不应存储在数据库中的数据,但是这些数据对于应用程序中的其他对象还是有用的。

Yes, you are right. It is an attribute (a field) of your model, but not in your database. You could use such attributes to keep data that shouldn't be saved in the database, but that somehow are useful for other objects in your application.

例如:为名为 password的模型字段定义访问器。然后,当有人设置此值时,您可以对其进行哈希处理并将其存储在数据库的相应字段中。

For example: you could define an accessor for a model field named "password". Then when someone sets this value, you hash it and store it in the appropriate field in the database.

这篇关于什么是datamapper中的attr_accessor-红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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