gpt4 book ai didi

ruby-on-rails - 我在 Rails 中使用了保留字吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:40 27 4
gpt4 key购买 nike

这是我的模型:

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 的记录>/p>

但是,如果我说 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 试图通过我遇到的错误告诉我什么。

最佳答案

根据这个: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 没问题,但我认为将表命名为“records”可能会给您带来错误。

关于ruby-on-rails - 我在 Rails 中使用了保留字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com