简体   繁体   English

如何在Ruby / Rails中指定私有或受保护的属性?

[英]How do you designate private or protected attributes in Ruby/Rails?

How do you designate private or protected attributes in Ruby/Rails? 如何在Ruby / Rails中指定私有或受保护的属性?

Are all DB fields automatically attributes, and don't need to be defined in the Model? 是否所有数据库字段都自动属性,并且不需要在模型中定义?

Any recommended tutorials? 任何推荐的教程?

Working in Rails 3.0.7. 在Rails 3.0.7中工作。

The title doesn't match the question. 标题与问题不符。

Yes, DB fields are automatically attributes (depending on what you mean by attribute; they're not simply @column_name as with attr_accessor ). 是的,数据库字段是自动属性(取决于属性的含义;它们不像 @column_name那样与attr_accessor )。

You can provide some level of accessibility by using attr_accessible and attr_protected , but that's for mass-assignment, not general access. 您可以使用attr_accessibleattr_protected提供某种级别的可访问性,但这是用于批量分配,而不是一般访问。

You can use attr_protected , attr_accessible or attr_readonly 您可以使用attr_protectedattr_accessibleattr_readonly

The attr_protected , attr_readonly and attr_accessible macros control what is accepted for mass-assignment. attr_protectedattr_readonlyattr_accessible宏控制接受质量分配的内容。 Read those links if you're not familiar with the difference between those three macros. 如果您不熟悉这三个宏之间的区别,请阅读这些链接。

Documentation of ActiveRecord model: ActiveRecord模型的文档:

http://api.rubyonrails.org/classes/ActiveRecord/Base.html http://api.rubyonrails.org/classes/ActiveRecord/Base.html

http://apidock.com/rails/ActiveRecord/Base http://apidock.com/rails/ActiveRecord/Base

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM