Demonstrates a bug with activerecord or maybe arel? generating invalid sql for MySQL with join conditions
Ruby JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
config
db
doc
lib
log
public
script
spec
test
vendor
.gitignore
.rspec
Gemfile
Gemfile.lock
README
Rakefile
config.ru

README

This demonstrates a bug in how has_many conditions work when the conditions 
are on a table that is also joined via has_many :through.

The object model is:

  Owner (:name => string)
    |
    |-> JoinTable
            |
            |-> Target (:name => string, :active => boolean)

The problem comes in the named scope on owner-- see spec/models/owner_spec.rb:13
Basically, when it creates the SQL, it looks like:

SELECT  DISTINCT owners.* 
FROM `owners` 
INNER JOIN `join_models` ON `join_models`.`owner_id` = `owners`.`id` AND targets.active = 't' 
INNER JOIN `targets` ON `targets`.`id` = `join_models`.`target_id` 
WHERE (targets.name = 'this is active') LIMIT 1

MySQL apparently requires that the targets table be joined before referencing it in the
first INNER JOIN clause. This is not true for sqlite3; I don't have postgresql installed
right now to check that.