【问题标题】:Difference between attributes and base_attributes in Ruby?Ruby中属性和base_attributes之间的区别?
【发布时间】:2013-12-16 19:34:43
【问题描述】:

我见过几个模型定义了一个静态方法

self.base_attributes
 {
    :object => []
 }
end 

还有一些模型定义了静态方法

self.attributes
  @@attributes = {}
end

属性和基础属性到底有什么区别?

【问题讨论】:

  • 你能指出源代码中的一些例子吗?

标签: ruby-on-rails ruby


【解决方案1】:

在您的示例中,在不了解代码的情况下,self.attributes 方法使用类变量 (@@attributes),这意味着您可以在运行时向其添加更多属性。

您的base_attributes 是硬编码的。我怀疑你看到的是这样的:

base_attributes.merge(attributes) 这也许是一种定义默认值的方式。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2012-10-03
    • 2011-06-22
    • 2012-08-27
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 2017-08-25
    相关资源
    最近更新 更多