datamapper - ruby​​ 中的 attr_accessor 是什么

发布于 2025-01-06 02:39:24 字数 549 浏览 4 评论 0原文

我是数据映射器的新手。我在这个论坛看到了这段代码。

    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 意味着什么..它是模型中的字段,但不是数据库中的字段..

谢谢

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

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..

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2025-01-13 02:39:24

是的你是对的。它是模型的属性(字段),但不在数据库中。您可以使用此类属性来保留不应保存在数据库中但对应用程序中的其他对象有用的数据。

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

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.

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.

a√萤火虫的光℡ 2025-01-13 02:39:24

看起来密码没有存储在数据库中,这很好。

仅当用户首次登录或更改密码时,密码才会存储在用户对象中。由于普通的 ruby​​ sinatra 应用程序本质上会在每次页面加载时启动,因此密码仅在密码被散列并放入数据库等时才存在。

换句话说,您通常可以预期获取密码的调用将会失败。仅当您仍在处理登录或密码更改事件时,它才有效。

It looks like the password is not stored in the database, which is good.

The password is stored in the user object only when they say first login or when they are changing their password. Since a normal ruby sinatra app essentially boots on each page load, the password is only around while it gets hashed and put into the db, etc.

In other words you can often expect that a call to obtain the password will fail. It will only work if you are still handling the login or password change event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文