Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11587 (new defect)

Opened 1 month ago

Last modified 2 weeks ago

has_many :through does not work with belongs_to

Reported by: jenda Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: 2.0.1
Severity: normal Keywords: association, through, belongs_to
Cc:

Description

Let's say we have 3 models:

class Group < ActiveRecord::Base
  has_many  :resources
  has_many  :users
end
 
class Resource < ActiveRecord::Base
  belongs_to :group
end
 
class User < ActiveRecord::Base
  belongs_to :group
  has_many   :resources,  :through => :group
end 

We want to access the resources of the group from the User model. When we try to access the resources association, Rails will reoprt an SQL error:

>> User.find(1).resources
User.find(1).resources
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'groups.group_id' in 'where clause': SELECT resources.* FROM resources  INNER JOIN groups ON resources.group_id = groups.id    WHERE ((groups.group_id = 1)) 
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:281:in `execute'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:481:in `select'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:55:in `select_all'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:532:in `find_by_sql'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1233:in `find_every'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:503:in `find'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_through_association.rb:140:in `find_target'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:133:in `load_target'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:55:in `reload'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:77:in `inspect'
        from /usr/lib/ruby/1.8/irb.rb:298:in `output_value'
        from /usr/lib/ruby/1.8/irb.rb:151:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:259:in `signal_status'
        from /usr/lib/ruby/1.8/irb.rb:147:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:70:in `start'
        from /usr/lib/ruby/1.8/irb.rb:69:in `catch'
        from /usr/lib/ruby/1.8/irb.rb:69:in `start'

There is a field named 'group_id' instead of 'id' in the WHERE clause.

Can anybody please confirm this ?

Attachments

test.tgz (4.0 kB) - added by jenda on 04/16/08 10:53:09.
FIles demonstrating the problem, test included, http://pastie.caboo.se/181601 patch included

Change History

04/16/08 09:11:27 changed by jeffrafter

I am seeing the same error all the way through 1.1.6. I made a quick workaround that can be seen here

http://pastie.caboo.se/181601

This manages to get you around the problem similar to the workaround for :include without changing the rest of the code significantly

04/16/08 10:53:09 changed by jenda

  • attachment test.tgz added.

FIles demonstrating the problem, test included, http://pastie.caboo.se/181601 patch included

04/16/08 11:04:00 changed by jenda

Thanks very much for reply,

tried to make the change, but it still isn't working. I uploaded the problematic files, so you can make experiments.

I am more like beginner so I don't think I can fix the error, but when I attempted to trace the problem I've noticed that in construct_conditions method there is call to the method construct_quoted_owner_attributes - this returns the hash containing bad field name 'group_id' instead of 'id' and then the invalid statement is constructed.

I see no problem in JOIN clause, so why modify the construct_joins method ?

05/04/08 01:40:30 changed by cduan

I think that the following patch should fix the problem. Just include it in environment.rb.

http://pastie.caboo.se/191201