GithubHelp home page GithubHelp logo

Comments (3)

siobhandougall avatar siobhandougall commented on July 19, 2024

I'm running into something similar while attempting to update to Rails 4.2.4 (from 4.1.6), and using MySQL. It's not a polymorphic relation, but it looks like a similar parsing error is happening. Here's a really contrived example:

# person.rb
class Person < ActiveRecord::Base
  has_many :active_emails, -> { where active: true }, class_name: 'Email'
  has_many :twitters
end

Say we want to find all people with active emails starting with "johndoe".

Person.joins(:active_emails).where('address like ?', 'johndoe%')

yields

SELECT `people`.* FROM `people` INNER JOIN `emails` ON `emails`.`person_id` = `people`.`id` AND `emails`.`active` = 1 WHERE (address like 'johndoe%')

So far, so good. Now let's say we want to find all people with either an active email starting with "johndoe" or a Twitter handle starting with "johndoe".

Person.joins(:active_emails).where('address like ?', 'johndoe%').union(Person.joins(:twitters).where('handle like ?', 'johndoe%'))

yields

SELECT `people`.* FROM ( (SELECT `people`.* FROM `people` INNER JOIN `emails` ON `emails`.`person_id` = `people`.`id` AND `emails`.`active` =  WHERE (address like 'johndoe%')) UNION (SELECT `people`.* FROM `people` INNER JOIN `twitters` ON `twitters`.`person_id` = `people`.`id` WHERE (handle like 'johndoe%')) ) people

Note that where it used to say, "AND emails.active = 1 WHERE...", the "1" has now mysteriously vanished, and this is no longer valid SQL.

This does not happen with sqlite3, presumably because the original syntax is different:

-- sqlite3 sanitizes out the parameter in the scope block
SELECT "people".* FROM "people" INNER JOIN "emails" ON "emails"."person_id" = "people"."id" AND "emails"."active" = ? WHERE (address like 'johndoe%')  [["active", "t"]]

from active_record_union.

brianhempel avatar brianhempel commented on July 19, 2024

I can reproduce on SQLite…

class User < ActiveRecord::Base
  has_many :posts
  has_many :drafts, -> { where draft: true  }, class_name: "Post"
end

class Post < ActiveRecord::Base
  belongs_to :user
end

User.joins(:drafts).union(User.where(id: 11)).to_sql
SELECT "users".* FROM (
  SELECT "users".*
  FROM "users"
  INNER JOIN "posts" ON "posts"."user_id" = "users"."id" AND "posts"."draft" = 11
  UNION
  SELECT "users".* FROM "users"
  WHERE "users"."id" =
) "users"

from active_record_union.

brianhempel avatar brianhempel commented on July 19, 2024

Thanks for the bug report with the examples. You've been credited in the README.

v1.1.1 is live on RubyGems with the fix in #7.

Let me know if you have any more problems.

from active_record_union.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.