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.

On Apr 14, 10:13 pm, James B. [email protected]
wrote:

I have the following models:

class Location < ActiveRecord::Base

belongs_to :entities
belongs_to :sites

should be belongs_to :site and belongs_to :entity

Fred

Frederick C. wrote:

� belongs_to � � � �:entities
� belongs_to � � � �:sites

should be belongs_to :site and belongs_to :entity

Fred

I had that to begin with but that gave this error:

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

Julian L. 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?

On 15 Apr 2008, at 14:22, James B. 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

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/

On 15 Apr 2008, at 14:53, James B. wrote:

You need to also change it wherever you used location.sites
./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 C. wrote:

On 15 Apr 2008, at 14:22, James B. 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:

<%=h location.site_id
%>
./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.

On 15 Apr 2008, at 15:31, James B. 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

Frederick C. 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:
63:


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:incompute_type’
vendor/rails/activerecord/lib/active_record/reflection.rb:125:in send' vendor/rails/activerecord/lib/active_record/reflection.rb:125:inklass’
vendor/rails/activerecord/lib/active_record/associations.rb:1602:in
initialize' vendor/rails/activerecord/lib/active_record/associations.rb:1498:innew’
vendor/rails/activerecord/lib/active_record/associations.rb:1498:in
build_join_association' vendor/rails/activerecord/lib/active_record/associations.rb:1481:inbuild’
vendor/rails/activerecord/lib/active_record/associations.rb:1484:in
build' vendor/rails/activerecord/lib/active_record/associations.rb:1483:ineach’
vendor/rails/activerecord/lib/active_record/associations.rb:1483:in
build' vendor/rails/activerecord/lib/active_record/associations.rb:1424:ininitialize’
vendor/rails/activerecord/lib/active_record/calculations.rb:180:in new' vendor/rails/activerecord/lib/active_record/calculations.rb:180:inconstruct_calculation_sql’
vendor/rails/activerecord/lib/active_record/calculations.rb:209:in
execute_simple_calculation' vendor/rails/activerecord/lib/active_record/calculations.rb:123:incalculate’
vendor/rails/activerecord/lib/active_record/calculations.rb:119:in
catch' vendor/rails/activerecord/lib/active_record/calculations.rb:119:incalculate’
vendor/rails/activerecord/lib/active_record/calculations.rb:46:in
count' vendor/rails/activerecord/lib/active_record/associations/has_many_association.rb:96:incount_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:inempty?’
app/views/entities/index.html.erb:64:in
_run_erb_47app47views47entities47index46html46erb' app/views/entities/index.html.erb:28:ineach’
app/views/entities/index.html.erb:28:in
_run_erb_47app47views47entities47index46html46erb' vendor/rails/actionpack/lib/action_view/base.rb:645:insend’
vendor/rails/actionpack/lib/action_view/base.rb:645:in
compile_and_render_template' vendor/rails/actionpack/lib/action_view/base.rb:367:inrender_template’
vendor/rails/actionpack/lib/action_view/base.rb:316:in render_file' vendor/rails/actionpack/lib/action_controller/base.rb:1101:inrender_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:inrender_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:inrender’
/usr/lib/ruby/1.8/benchmark.rb:293:in measure' vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:inrender’
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:131:in
send' vendor/rails/actionpack/lib/action_controller/mime_responds.rb:131:incustom’
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:156:in
call' vendor/rails/actionpack/lib/action_controller/mime_responds.rb:156:inrespond’
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:150:in
each' vendor/rails/actionpack/lib/action_controller/mime_responds.rb:150:inrespond’
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:inperform_action_without_filters’
vendor/rails/actionpack/lib/action_controller/filters.rb:699:in
call_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:691:inperform_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:inmeasure’
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' vendor/rails/actionpack/lib/action_controller/rescue.rb:199:inperform_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:incache’
vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in cache' vendor/rails/actionpack/lib/action_controller/caching.rb:677:inperform_action’
vendor/rails/actionpack/lib/action_controller/base.rb:524:in send' vendor/rails/actionpack/lib/action_controller/base.rb:524:inprocess_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:inprocess’
vendor/rails/actionpack/lib/action_controller/base.rb:388:in process' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:171:inhandle_request’
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:115:in
dispatch' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:126:indispatch_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:inprocess’
/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:inprocess’
/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:ineach’
/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:inrun’
/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:innew’
/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:ininitialize’
/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:inrun’
/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:ineach’
/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:inrun’
/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:inload’
vendor/rails/activesupport/lib/active_support/dependencies.rb:489:in
load' vendor/rails/activesupport/lib/active_support/dependencies.rb:342:innew_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:ingem_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:inrequire’
vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in
new_constants_in' vendor/rails/activesupport/lib/active_support/dependencies.rb:496:inrequire’
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

Frederick C. wrote:

On 15 Apr 2008, at 15:31, James B. 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?

If I use the plural forms then I get this message instead, but at
exactly the same places:

uninitialized constant Location::Sites

I cannot see what it is that I am doing wrong here. entity.locations is
the collection of locations relating to entities, that might be empty,
while location.site should be the the site related to a location in that
collection. I cannot tell where this Sites is coming from. I have
checked site.rb and the class name is defined as this:

class Site < ActiveRecord::Base

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

SO, what is going on?

On 15 Apr 2008, at 15:56, James B. 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

Frederick C. wrote:

On 15 Apr 2008, at 15:56, James B. 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:

Listing sites


./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.

On 15 Apr 2008, at 16:18, James B. wrote:

‘vendor’

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

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 C. wrote:

On 15 Apr 2008, at 16:18, James B. wrote:

‘vendor’

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

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

Ahhhhhrrrrggg… that was it.

Thank you!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs