This project is archived and is in readonly mode.
ActiveRecord 3 Regression: Certain includes hashes fail on AR3 that work on AR2.
-
Neeraj Singh
- Importance changed from to Low
works for me with rails edge.
def self.lab Project.includes(:tasks => :users).where({:users => {:name => 'John2'}}) endgenerates following sql
SELECT "projects"."id" AS t0_r0, "projects"."name" AS t0_r1, "projects"."created_at" AS t0_r2, "projects"."updated_at" AS t0_r3, "tasks"."id" AS t1_r0, "tasks"."name" AS t1_r1, "tasks"."project_id" AS t1_r2, "users"."id" AS t2_r0, "users"."name" AS t2_r1 FROM "projects" LEFT OUTER JOIN "tasks" ON "tasks"."project_id" = "projects"."id" LEFT OUTER JOIN "tasks_users" ON "tasks_users"."task_id" = "tasks"."id" LEFT OUTER JOIN "users" ON "users"."id" = "tasks_users"."user_id" WHERE ("users"."name" = 'John2')ActiveRecord::Schema.define(:version => 20100714212039) do create_table "projects", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "tasks", :force => true do |t| t.string "name" t.integer "project_id", :null => false end create_table "tasks_users", :id => false, :force => true do |t| t.integer "task_id", :null => false t.integer "user_id", :null => false end create_table "users", :force => true do |t| t.string "name" end end class Project < ActiveRecord::Base has_many :tasks, :uniq => true def self.setup Project.delete_all Task.delete_all User.delete_all pr2 = Project.create!(:name => 'pr2') task2 = Task.new(:name => 'task2') task2.project_id = pr2.id task2.save! task2.users.create(:name => 'John1') task2.users.create(:name => 'John2') task2.users.create(:name => 'John3') end def self.lab Project.includes(:tasks => :users).where({:users => {:name => 'John2'}}) end end class Task < ActiveRecord::Base belongs_to :project has_and_belongs_to_many :users, :uniq => true end class User < ActiveRecord::Base has_and_belongs_to_many :tasks end -
clocksarestupid
Neeraj, change your query to the following:
Project.includes([{:tasks => :users}, :tasks]).where({:users => {:name => 'John2'}})
Then it should fail.
-
Neeraj Singh
Thanks @clocks. Yes in that case query is indeed failing.
-
clocksarestupid
So it's missing a join table. In Rails 2.3.x it has 3 joins that it's performing and in Rails 3 it's missing one.
I've attached a simple file that is easier to debug with. There's a toggle flag at the top that will switch between ActiveRecord 3 and ActiveRecord 2. It will construct an in-memory SQLite3 database and try to perform the query.
-
clocksarestupid
I've got the fix. There was an incorrect equality check in the joins that was excluding the last join. I've attached a patch that fixes the problem and also adds a unit test to verify that no exceptions are thrown.
-
clocksarestupid
- Tag changed from activerecord rails3, regression to activerecord rails3, patch activerecord, fix, fixed, regression
-
Andrea Campi
- Tag changed from activerecord rails3, patch activerecord, fix, fixed, regression to activerecord rails3, activerecord, fix, fixed, patch, regression
bulk tags cleanup
-
Santiago Pastorino
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale
