Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Error] When using joining with on, DSL refers the association to wrong table name or undefined table alias #65

Open
Looooong opened this issue May 5, 2017 · 6 comments

Comments

@Looooong
Copy link

Looooong commented May 5, 2017

Issue

Course.joining{ enrollments.on(id == enrollments.course_id) }.where.has{ enrollments.progress == 100 }

will produce the following query:

SELECT "courses".*
FROM "courses"
    INNER JOIN "enrollments" ON "courses"."id" = "enrollments"."course_id"
WHERE "enrollments_courses"."progress" = 100.0 // <-- Error here

Notice the WHERE condition, it's enrollments_courses instead of enrollments, or the table alias enrollments_courses is not defined.

On the other hand, this code generates query correctly:

Course.joining{ enrollments.on(id == enrollments.course_id) }.where(Enrollment.arel_table[:progress].eq(100.0))

The expected query is:

SELECT "courses".*
FROM "courses"
    INNER JOIN "enrollments" ON "courses"."id" = "enrollments"."course_id"
WHERE "enrollments"."progress" = 100.0

I'm using Rails version 4.2.7.1, Ruby version 2.3.3, Baby Squeel version 1.1.4.

@rzane
Copy link
Owner

rzane commented May 8, 2017

Hmm this is interesting. Could you create a reproducible test case based on this example: https://github.com/rzane/baby_squeel/blob/master/ISSUE_TEMPLATE.md

@Looooong
Copy link
Author

Looooong commented May 9, 2017

require 'bundler/inline'
require 'minitest/spec'
require 'minitest/autorun'

gemfile true do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 4.2.7.1' # which Active Record version?
  gem 'sqlite3'
  gem 'baby_squeel', github: 'rzane/baby_squeel'
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :courses, force: true do |t|
  end

  create_table :enrollments, force: true do |t|
    t.references :course, index: true, foreign_key: true
    t.float :progress, default: 0, null: false
  end
end

class Course < ActiveRecord::Base
  has_many :enrollments
end

class Enrollment < ActiveRecord::Base
  belongs_to :course
end

class BabySqueelTest < Minitest::Spec
  it 'works' do
    scope = Course.joining{ enrollments.on(id == enrollments.course_id) }.where.has{ enrollments.progress == 100 }

    scope.to_sql.must_equal <<-SQL.squish
      SELECT "courses".*
      FROM "courses"
          INNER JOIN "enrollments" ON "courses"."id" = "enrollments"."course_id"
      WHERE "enrollments"."progress" = 100.0
    SQL
  end
end

Test result

Run options: --seed 35788

# Running:

F

Finished in 0.135758s, 7.3660 runs/s, 7.3660 assertions/s.

  1) Failure:
BabySqueelTest#test_0001_works [test.rb:36]:
--- expected
+++ actual
@@ -1 +1 @@
-"SELECT \"courses\".* FROM \"courses\" INNER JOIN \"enrollments\" ON \"courses\".\"id\" = \"enrollments\".\"course_id\" WHERE \"enrollments\".\"progress\" = 100.0"
+"SELECT \"courses\".* FROM \"courses\" INNER JOIN \"enrollments\" ON \"courses\".\"id\" = \"enrollments\".\"course_id\" WHERE \"enrollments_courses\".\"progress\" = 100.0"


1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

@Looooong
Copy link
Author

Looooong commented May 9, 2017

The problem also exists on Rails 5

@passalini
Copy link

+1 rails 4.1.0

@kbighorse
Copy link

+1 rails 5.2.2.1, baby_squeel 1.3.1

@kbighorse
Copy link

kbighorse commented May 24, 2019

using @Looooong's syntax in the meantime:

Course.joining{ enrollments.on(id == enrollments.course_id)}.where(Enrollment.arel_table[:progress].eq(100.0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants