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? -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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 HostYou 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.