# Strange behavior of :through association

**URL:** <https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837>\
**Category:** Rails\
**Created:** [June 19, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837 "2006-06-19T20:43:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [June 19, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837/1 "2006-06-19T20:43:01Z")

</div>

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:in`initialize’  
/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:in`hosts’  
/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:in`init\_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:in`send’  
/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:in`perform\_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:in`measure’  
/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:in`perform\_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:in`process\_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:in`process’  
/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:in`handle\_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:in`service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/lib/ruby/1.8/webrick/server.rb:172:in`start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:161:in `start' /usr/lib/ruby/1.8/webrick/server.rb:161:in`start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/lib/ruby/1.8/webrick/server.rb:92:in`each’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in`start’  
/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:in`dispatch’  
/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:in`require’  
/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:in`require\_\_’  
/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:in`require’  
script/server:3

Request

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

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

Ideas?

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [June 19, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837/2 "2006-06-19T22:46:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [June 19, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837/3 "2006-06-19T22:59:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [June 20, 2006, 12:10am UTC](https://www.ruby-forum.com/t/strange-behavior-of-through-association/63837/4 "2006-06-20T00:10:05Z")

</div>

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](http://blog.hasmanythrough.com)
