Uninitialized Constant

I have the following models:

class Entity < ActiveRecord::Base
...
  has_many :locations, :dependent => :destroy,
                                  :include => :sites

  has_many :sites, :through => :locations

class Site < ActiveRecord::Base

  has_many :locations
  has_many :entities, :through => :locations

class Location < ActiveRecord::Base

  belongs_to :entities
  belongs_to :sites

In views/entities/index.html.erb I have this:

      <% if entity.locations.empty? -%>
            <%= link_to 'Add Locations',
new_entity_locations_path(entity) -%>
      <% else -%>
            <%= link_to 'Edit Locations', entity_locations_path(entity)
-%>
      <% end -%>

And in entities_controller.rb I have this:

10 def index
11 @entities = Entity.find(:all)
12
13 respond_to do |format|
14 format.html # index.html.erb
15 format.xml { render :xml => @entities }
16 end
17 end

When I look at views/entities/index I get this error:

uninitialized constant Location::Sites

...
64: <% if entity.locations.empty? -%>
65: <%= link_to 'Add Locations',
new_entity_locations_path(entity) -%>
66: <% else -%>

...
app/controllers/entities_controller.rb:13:in `index'

I am evidently overlooking something obvious but for the life of me I
cannot see what. Another pair of eyes is most welcome.

should be belongs_to :site and belongs_to :entity

Fred

It should be singular, not plural. (ie no "s").

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #4 coming soon!
http://sensei.zenunit.com/

Julian Leviston wrote:

It should be singular, not plural. (ie no "s").

Julian.

I had that originally and got a different error at the same locations:

class Location < ActiveRecord::Base

  belongs_to :entity
  belongs_to :site

Association named 'sites' was not found; perhaps you misspelled it?

You need to also change it wherever you used location.sites

Fred

Frederick Cheung wrote:

belongs_to :entity
belongs_to :site

Association named 'sites' was not found; perhaps you misspelled it?

You need to also change it wherever you used location.sites

Fred

$ find ./ -print | xargs grep 'locations.sites'
$ find ./ -print | xargs grep 'location.sites'
$ find ./ -print | xargs grep 'locations.site'
$ find ./ -print | xargs grep 'location.site'
... (.svn stuff removed)
./app/views/locations/index.html.erb: <td><%=h location.site_id
%></td>
./app/views/locations/new.html.erb: :object =>
@location.site -%>
./app/views/locations/show.html.erb: <%=h @location.site_id %>
$

I cannot seem to find anywhere that I have done this.

Frederick Cheung wrote:

belongs_to :entity
belongs_to :site

Association named 'sites' was not found; perhaps you misspelled it?

You need to also change it wherever you used location.sites

Fred

$ find ./ -print | xargs grep 'locations.sites'
$ find ./ -print | xargs grep 'location.sites'
$ find ./ -print | xargs grep 'locations.site'
$ find ./ -print | xargs grep 'location.site'
... (.svn stuff removed)
./app/views/locations/index.html.erb: <td><%=h location.site_id
%></td>
./app/views/locations/new.html.erb: :object =>
@location.site -%>
./app/views/locations/show.html.erb: <%=h @location.site_id %>
$

I cannot seem to find anywhere that I have done this.

Find the line causing that error (it should be in the stack trace).

Fred

Frederick Cheung wrote:
t seem to find anywhere that I have done this.

Find the line causing that error (it should be in the stack trace).

Fred

NameError in Entities#index

Showing entities/index.html.erb where line #64 raised:

uninitialized constant Location::Sites

Extracted source (around line #64):

61: <% end -%>
62: </td>
63: <td>
64: <% if entity.locations.empty? -%>
65: <%= link_to 'Add Locations',
new_entity_locations_path(entity) -%>
66: <% else -%>
67: <%= link_to 'Edit Locations', entity_locations_path(entity)
-%>

vendor/rails/activesupport/lib/active_support/dependencies.rb:478:in
`const_missing'
vendor/rails/activerecord/lib/active_record/base.rb:1764:in
`compute_type'
vendor/rails/activerecord/lib/active_record/reflection.rb:125:in `send'
vendor/rails/activerecord/lib/active_record/reflection.rb:125:in `klass'
vendor/rails/activerecord/lib/active_record/associations.rb:1602:in
`initialize'
vendor/rails/activerecord/lib/active_record/associations.rb:1498:in
`new'
vendor/rails/activerecord/lib/active_record/associations.rb:1498:in
`build_join_association'
vendor/rails/activerecord/lib/active_record/associations.rb:1481:in
`build'
vendor/rails/activerecord/lib/active_record/associations.rb:1484:in
`build'
vendor/rails/activerecord/lib/active_record/associations.rb:1483:in
`each'
vendor/rails/activerecord/lib/active_record/associations.rb:1483:in
`build'
vendor/rails/activerecord/lib/active_record/associations.rb:1424:in
`initialize'
vendor/rails/activerecord/lib/active_record/calculations.rb:180:in `new'
vendor/rails/activerecord/lib/active_record/calculations.rb:180:in
`construct_calculation_sql'
vendor/rails/activerecord/lib/active_record/calculations.rb:209:in
`execute_simple_calculation'
vendor/rails/activerecord/lib/active_record/calculations.rb:123:in
`calculate'
vendor/rails/activerecord/lib/active_record/calculations.rb:119:in
`catch'
vendor/rails/activerecord/lib/active_record/calculations.rb:119:in
`calculate'
vendor/rails/activerecord/lib/active_record/calculations.rb:46:in
`count'
vendor/rails/activerecord/lib/active_record/associations/has_many_association.rb:96:in
`count_records'
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:108:in
`size'
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:121:in
`empty?'
app/views/entities/index.html.erb:64:in
`_run_erb_47app47views47entities47index46html46erb'
app/views/entities/index.html.erb:28:in `each'
app/views/entities/index.html.erb:28:in
`_run_erb_47app47views47entities47index46html46erb'
vendor/rails/actionpack/lib/action_view/base.rb:645:in `send'
vendor/rails/actionpack/lib/action_view/base.rb:645:in
`compile_and_render_template'
vendor/rails/actionpack/lib/action_view/base.rb:367:in `render_template'
vendor/rails/actionpack/lib/action_view/base.rb:316:in `render_file'
vendor/rails/actionpack/lib/action_controller/base.rb:1101:in
`render_for_file'
vendor/rails/actionpack/lib/action_controller/base.rb:858:in
`render_with_no_layout'
vendor/rails/actionpack/lib/action_controller/base.rb:872:in
`render_with_no_layout'
vendor/rails/actionpack/lib/action_controller/layout.rb:261:in
`render_without_benchmark'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in
`render'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in
`render'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:131:in
`send'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:131:in
`custom'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:156:in
`call'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:156:in
`respond'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:150:in
`each'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:150:in
`respond'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:107:in
`respond_to'

app/controllers/entities_controller.rb:13:in `index'

vendor/rails/actionpack/lib/action_controller/base.rb:1159:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:1159:in
`perform_action_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:699:in
`call_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:691:in
`perform_action_without_benchmark'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
vendor/rails/actionpack/lib/action_controller/rescue.rb:199:in
`perform_action_without_caching'
vendor/rails/actionpack/lib/action_controller/caching.rb:678:in
`perform_action'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in
`cache'
vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'
vendor/rails/actionpack/lib/action_controller/caching.rb:677:in
`perform_action'
vendor/rails/actionpack/lib/action_controller/base.rb:524:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:524:in
`process_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:687:in
`process_without_session_management_support'
vendor/rails/actionpack/lib/action_controller/session_management.rb:123:in
`process'
vendor/rails/actionpack/lib/action_controller/base.rb:388:in `process'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:171:in
`handle_request'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:115:in
`dispatch'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:126:in
`dispatch_cgi'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:9:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:76:in
`process'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in
`synchronize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in
`process'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:159:in
`process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in `each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in
`process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in
`initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in
`initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:282:in
`run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in
`each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in
`run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:128:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/command.rb:212:in
`run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:281
vendor/rails/activesupport/lib/active_support/dependencies.rb:489:in
`load'
vendor/rails/activesupport/lib/active_support/dependencies.rb:489:in
`load'
vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in
`new_constants_in'
vendor/rails/activesupport/lib/active_support/dependencies.rb:489:in
`load'
vendor/rails/railties/lib/commands/servers/mongrel.rb:64
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
vendor/rails/activesupport/lib/active_support/dependencies.rb:496:in
`require'
vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in
`new_constants_in'
vendor/rails/activesupport/lib/active_support/dependencies.rb:496:in
`require'
vendor/rails/railties/lib/commands/server.rb:39
script/server:3:in `require'
script/server:3

The code in entities__controller around line 13 is:

10: def index
11: @entities = Entity.find(:all)
12:
13: respond_to do |format|
14: format.html # index.html.erb
15: format.xml { render :xml => @entities }
16: end
17: end

Which probably means there's something not right in location.rb

Fred

Frederick Cheung wrote:

Showing entities/index.html.erb where line #64 raised:

uninitialized constant Location::Sites

Which probably means there's something not right in location.rb

Fred

This is the entirety of location.rb:

class Location < ActiveRecord::Base

belongs_to :entity
belongs_to :site

end

If I use the singular fors for entity and site in the belongs_to, which
is what I did initially, then I get this error in entities/index at line
64:

Association named 'sites' was not found; perhaps you misspelled it?

That means that something is still using the association names sites (eg an :include => :sites). Track it down.

Fred

Frederick Cheung wrote:

Fred

If I use the singular fors for entity and site in the belongs_to,
which
is what I did initially, then I get this error in entities/index at
line
64:

Association named 'sites' was not found; perhaps you misspelled it?

That means that something is still using the association names sites
(eg an :include => :sites). Track it down.

Fred

I grep'ed my entire project and found only these references to sites:

$ find ./ -print | xargs grep sites | grep -v '.svn' | grep -v 'vendor'

This stuff is just test fixtures and migrations. As far as I am aware
none of this should have any effect on running code:

./test/fixtures/sites.yml:# Table name: sites
./db/migrate/022_create_sites.rb: create_table :sites do |t|
./db/migrate/022_create_sites.rb: add_index :sites,
[:site_country_code, :site_postal_code],
./db/migrate/022_create_sites.rb: :name =>
:idxR_sites_country_postal_codes,
./db/migrate/022_create_sites.rb: remove_index :sites, :name =>
:idxR_sites_country_postal_codes
./db/migrate/022_create_sites.rb: drop_table :sites
Binary file ./db/development.sqlite3 matches
./db/schema.rb: create_table "sites", :force => true do |t|
./db/schema.rb: add_index "sites", ["site_country_code",
"site_postal_code"], :name => "idxR_sites_country_postal_codes"
Binary file ./app/models/.site.rb.swp matches

This is the stuff of interest:

./app/models/entity.rb: :include =>
:sites
./app/models/entity.rb: has_many :sites, :through =>
:locations
./app/models/site.rb:# Table name: sites

Is the :include => :sites in entity.rb the problem? If so then why?

./app/controllers/sites_controller.rb: # GET /sites
./app/controllers/sites_controller.rb: # GET /sites.xml
./app/controllers/sites_controller.rb: @sites = Site.find(:all)
./app/controllers/sites_controller.rb: format.xml { render :xml =>
@sites }
./app/controllers/sites_controller.rb: # GET /sites/1
./app/controllers/sites_controller.rb: # GET /sites/1.xml
./app/controllers/sites_controller.rb: # GET /sites/new
./app/controllers/sites_controller.rb: # GET /sites/new.xml
./app/controllers/sites_controller.rb: # GET /sites/1/edit
./app/controllers/sites_controller.rb: # POST /sites
./app/controllers/sites_controller.rb: # POST /sites.xml
./app/controllers/sites_controller.rb: # PUT /sites/1
./app/controllers/sites_controller.rb: # PUT /sites/1.xml
./app/controllers/sites_controller.rb: # DELETE /sites/1
./app/controllers/sites_controller.rb: # DELETE /sites/1.xml
./app/controllers/sites_controller.rb: format.html {
redirect_to(sites_url) }
./app/views/sites/edit.html.erb:<%= link_to 'Back', sites_path %>
./app/views/sites/index.html.erb:<h1>Listing sites</h1>
./app/views/sites/index.html.erb:<% for site in @sites %>
./app/views/sites/new.html.erb:<%= link_to 'Back', sites_path %>
./app/views/sites/show.html.erb:<%= link_to 'Back', sites_path %>
./app/views/locations/new.html.erb: <%= render :partial =>
'sites/site_detail',
./config/routes.rb: map.resources :sites do |site|

You assistance has been greatly appreciated.

entity.rb looks like this:

class Entity < ActiveRecord::Base
  has_one :client, :dependent => :destroy
  has_one :vendor, :dependent => :destroy

  has_many :locations, :dependent => :destroy,
                                  :include => :sites

  has_many :sites, :through => :locations

Yes. it's a problem because you're saying 'when you load location, also load the association sites". But location doesn't have an association called sites (it does have one called site)

Fred

Frederick Cheung wrote: