TheDelChop
TheDelChop

Reputation: 7998

Am I using a reserved word in Rails?

Here are my models:

class Record < ActiveRecord::Base

  belongs_to :user

  belongs_to :directory
end

class Directory < ActiveRecord::Base

  has_many :records
end

I can easily call Directory.first.records and get back all the records with a directory_id equal to Directory.first.id

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.

Upvotes: 2

Views: 962

Answers (1)

corroded
corroded

Reputation: 21604

According to this: http://wiki.rubyonrails.org/rails/pages/reservedwords

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

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

Upvotes: 2

Related Questions