由于defined?存在问题(请参考@Jiggneshh Gohel的评论),也许您可以检查models目录中的文件名。 files = Dir[Rails.root + 'app/models/*.rb'] models = files.map{ |m| File.basename(m, '.rb').camelize } models.include? "User" => true
$ rails c Loading development environment (Rails 4.2.0) 2.2.1 :001 > defined?(User) => "constant" 2.2.1 :002 > defined?(AuthenticationToken) => nil 2.2.1 :003 > AuthenticationToken => AuthenticationToken (call 'AuthenticationToken.connection' to establish a connection) 2.2.1 :004 > defined?(AuthenticationToken) => "constant" 2.2.1 :005 >
- Jignesh Goheldefined?
运算符更多信息的链接:http://ruby-doc.com/docs/ProgrammingRuby/html/tut_expressions.html#UG - saudes