This project is archived and is in readonly mode.

#6007 ✓duplicate
Peter Arzhintar

In Rails 2.3.10 :has_one :through using :include can generate invalid sql

Reported by Peter Arzhintar | November 18th, 2010 @ 07:28 PM

It looks like this bug, or one very similar to it, still exists: https://rails.lighthouseapp.com/projects/8994/tickets/1167-has_one-...

Using this example code:

class Season < ActiveRecord::Base
  belongs_to :tv_show
  has_many :episodes
end

class TvShow < ActiveRecord::Base
  has_many :seasons
end

class Episode < ActiveRecord::Base
  belongs_to :season
  has_one :tv_show, :through=>:season
end

Episode.find :all, :include=>:tv_show, :conditions=>['tv_shows.id = ?',85110]

I get this error. It looks like the episodes and seasons tables have been swapped:

Mysql::Error: Unknown column 'seasons.season_id' in 'on clause': SELECT `episodes`.`id` FROM `episodes`  LEFT OUTER JOIN `seasons` ON (`episodes`.`id` = `seasons`.`season_id`)  LEFT OUTER JOIN `tv_shows` ON (`tv_shows`.`id` = `seasons`.`tv_show_id`) WHERE (tv_shows.id = 85110)
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/connection_adapters/mysql_adapter.rb:324:in `execute'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/connection_adapters/mysql_adapter.rb:639:in `select'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/associations.rb:1622:in `select_all_rows'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/associations.rb:1401:in `find_with_associations'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/associations.rb:1399:in `catch'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/associations.rb:1399:in `find_with_associations'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/base.rb:1580:in `find_every'
    from /Users/shawn42/.rvm/gems/ruby-1.8.7-p299@rails310/gems/activerecord-2.3.10/lib/active_record/base.rb:619:in `find'
    from foo.rb:21

Comments and changes to this ticket

  • Jon Leighton

    Jon Leighton December 21st, 2010 @ 08:14 PM

    • State changed from “new” to “duplicate”
    • Importance changed from “” to “Low”

    Hi,

    Thanks for the bug report. I've tested your example against master and it is fixed these days. I believe this is a duplicate of #2801.

    Thanks,
    Jon

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages