Strange behavior of :through association

class Host < ActiveRecord::Base
belongs_to :person
end

class House < ActiveRecord::Base
has_many :people, :dependent => true
has_many :hosts, :through => :person
end

class Person < ActiveRecord::Base
belongs_to :house
has_many :hosts, :dependent => true
end

Now when I try to do:
@hosts = House.find_by_id(params[:namas]).hosts
from my controller I get this:
ActiveRecord::HasManyThroughAssociationNotFoundError in
TinkloController#index

Could not find the association :person in model Host

RAILS_ROOT: script/…/config/…

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:169:in
check_validity!' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_through_association.rb:6:ininitialize’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in
new' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:inhosts’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:871:in
hosts' #{RAILS_ROOT}/app/controllers/tinklo_controller.rb:17:ininit_vars’
#{RAILS_ROOT}/app/controllers/tinklo_controller.rb:3:in index' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:inmeasure’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:inprocess_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:inprocess’
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:inhandle_dispatch’
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
service' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/lib/ruby/1.8/webrick/server.rb:172:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:161:in start' /usr/lib/ruby/1.8/webrick/server.rb:161:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/lib/ruby/1.8/webrick/server.rb:82:instart’
/usr/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:indispatch’
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in
require__' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
require' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire__’
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:inrequire’
script/server:3

Request

Parameters: {“namas”=>“39”}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

Ideas?

I’m only guessing but I wonder if :through has problems with
people/person as a plural. Do you also get the problem if you use a name
that has a standard plural with an s added?

Julian

Another thought. What is “find_by_id”? I can’t find this in the Rails
documentation or in any source files in my Rails installation. It is
mentioned in various places though according to Google. Strange.

You could try using find(params[:namas]) and possible checking the
result before trying for the through record.

Finally, do your database tables have the correct foreign key fields
(house_id, person_id)?

Julian

Artūras Šlajus wrote:

class Host < ActiveRecord::Base
belongs_to :person
end

class House < ActiveRecord::Base
has_many :people, :dependent => true
has_many :hosts, :through => :person
end

class Person < ActiveRecord::Base
belongs_to :house
has_many :hosts, :dependent => true
end

Now when I try to do:
@hosts = House.find_by_id(params[:namas]).hosts
from my controller I get this:
ActiveRecord::HasManyThroughAssociationNotFoundError in
TinkloController#index

Could not find the association :person in model Host

You don’t have a :person has_many to go through. Use the :people name,
that’s what you declared in the previous line. Also, :dependent should
be :destroy, as “true” has been deprecated as a valid option.

class House < ActiveRecord::Base
has_many :people, :dependent => :destroy
has_many :hosts, :through => :people
end


Josh S.
http://blog.hasmanythrough.com

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