【发布时间】:2014-11-09 14:59:10
【问题描述】:
我刚刚开始学习 Rails。我有一个测试项目,我用一些现有的示例数据向 Post 模型类添加了一个实例方法和一个类方法。
class Post < ActiveRecord::Base
has_many :comments
attr_accessor :region
def sport
puts "Football"
end
def self.region
puts "West"
end
end
当我运行 Post.first.sport 时,我正确地得到了“足球”
但是当我运行 Post.first.region 时我得到了 nil。为什么 Rails 控制台不返回“West”?
谢谢!
【问题讨论】:
-
因为 Post.first 返回的 Post 实例没有 #region 实例方法。
标签: ruby-on-rails ruby class activerecord model