以下是我的模特:
class Record < ActiveRecord::Base
belongs_to :user
belongs_to :directory
end
class Directory < ActiveRecord::Base
has_many :records
end
我可以轻松致电Directory.first.records
并取回directory_id
等于Directory.first.id
但是,如果我说Record.first.directory
我得到以下错误:
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
我是否在这里使用保留?我不确定rails / ruby试图告诉我我得到的错误。
答案 0 :(得分:2)
根据这个: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没问题,但我认为命名你的表'记录'可能会给你这个错误。