Я использую зарезервированное слово в Rails? - PullRequest
2 голосов
/ 02 июля 2011

Вот мои модели:

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 ​​пытается сообщить мне с ошибкой, которую я получаю.

1 Ответ

2 голосов
/ 02 июля 2011

В соответствии с этим: 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' может приводить к этой ошибке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...