简体   繁体   English

如何从Rails控制台中的ActiveRecord类访问此哈希?

[英]How does one access this hash from an ActiveRecord class in the Rails console?

I have a simple hash I created to make it easier to do dictionary type lookups. 我创建了一个简单的哈希值,以使其更容易进行字典类型查找。 For instance: 例如:

class Numbers < ActiveRecord::Base

   MY_TYPES {
      one: => 1,
      two: => 2
   }

end

Is it not possible in the rails console to access this hash via Numbers.MY_TYPES[:one] ? 在Rails控制台中无法通过Numbers.MY_TYPES[:one]访问此哈希吗?

Try 尝试

Numbers::MY_TYPES[:one]

Why: MY_TYPES is not a method of class Numbers. 原因:MY_TYPES不是Numbers类的方法。 It is a constant. 这是一个常数。 So you use Numbers::MY_TYPES as the reference 因此,您使用Numbers :: MY_TYPES作为参考

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

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