我使用的保留字Rails的? [英] Am I using a reserved word in Rails?

查看:112
本文介绍了我使用的保留字Rails的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模型:

class Record < ActiveRecord::Base

  belongs_to :user

  belongs_to :directory
end

class Directory < ActiveRecord::Base

  has_many :records
end

我可以方便地调用 Directory.first.records ,并取回了 directory_id 等于<所有记录code> Directory.first.id

但是,如果我说 Record.first.directory 我得到以下errror:

However, if I say Record.first.directory I get the following errror:

NoMethodError: undefined method `const_defined?' for #<Record:0x00000108398da8>
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activemodel-3.0.7/lib/active_model/attribute_methods.rb:367:in `method_missing'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/attribute_methods.rb:46:in `method_missing'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:376:in `local_const_defined?'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:497:in `block in load_missing_constant'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/relation.rb:98:in `block in any?'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/relation.rb:98:in `each'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/relation.rb:98:in `any?'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/relation.rb:98:in `any?'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:497:in `load_missing_constant'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `each'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `const_missing'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/inflector/methods.rb:124:in `block in constantize'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/inflector/methods.rb:123:in `each'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/inflector/methods.rb:123:in `constantize'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:528:in `block in <class:Reference>'
... 2 levels...
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:538:in `get'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:551:in `constantize'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/base.rb:1191:in `block in compute_type'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/base.rb:1189:in `each'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/base.rb:1189:in `compute_type'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/reflection.rb:162:in `klass'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/associations/belongs_to_association.rb:59:in `find_target'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:241:in `load_target'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:118:in `reload'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/activerecord-3.0.7/lib/active_record/associations.rb:1442:in `block in association_accessor_methods'
from (irb):2
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /Users/thedelchop/.rvm/gems/ruby-1.9.2-p180@school_cnxt/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p180 :003 > exit

我使用保留这里的东西?我不知道是什么轨/红宝石是想告诉我与我得到的错误。

Am I using a reserved for here or something? I'm not sure what rails/ruby is trying to tell me with the error that I'm getting.

推荐答案

根据这个: HTTP: //wiki.rubyonrails.org/rails/pages/reservedwords

records – a table named records seemed to cause duplicate entries to be found by find

而Directory.first.records是好的,我认为你的命名表'的记录可能给你的错误。

while Directory.first.records is okay, i think naming your table 'records' might be giving you that error.

这篇关于我使用的保留字Rails的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆