Join Table Conditions Broken
Reported by Avi Flombaum | 2009-02-02 21:11:49 UTC
So Join Table Conditions don't work when using ar-extensions 0.8.0 and Rails 2.2.2
class Article < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :articles
end
# Get all the users that have published articles
User.find(:all, :joins => :article,
:conditions => { :articles => { :published => true } })
Returns:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'users.articles' in 'where clause': SELECT 'users'.* FROM 'users' INNER JOIN 'articles' ON 'users'.id = 'articles'.user_id WHERE 'users'.'articles' = '--- \n- :published\n- true\n'
Comments and changes to this ticket
-

Avi Flombaum 2009-02-08 16:58:46 UTC
- Assigned user set to Zach Dennis
-

Zach Dennis 2009-02-09 20:03:07 UTC
This is fixed in commit a74cc8aa70172507730484edf26ed0bcc2ae1055 . I'll push out a gem soon.
-

Zach Dennis 2009-02-09 20:13:07 UTC
- State changed from new to resolved
ar-extensions 0.8.1 has just been released. It should show up at a rubygem mirror shortly.
-

Alan Peabody 2009-03-04 12:40:52 UTC
I just installed ar-extensions 0.8.1 on a Rails 2.2.2 project and have been getting errors on join table conditions.
class Ticket < ActiveRecord::Base belongs_to :division end class Division < ActiveRecord::Base has_many :tickets end # Find all tickets by divisions with a name like %llc% Ticket.find(:all, :joins => :division, :conditions => {:divisions => {:name_contains => "hq"}})Returns:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'companies.name_contains' in 'where clause': SELECT
tickets.* FROMticketsINNER JOINdivisionsONdivisions.id =tickets.division_id INNER JOINcompaniesONcompanies.id =divisions.company_id WHERE (companies.name_contains= 'hq') -

Avi Flombaum 2009-03-04 12:57:19 UTC
That's weird - where is "companies" coming from in that SQL - I only see Ticket and Division. I'm going to test this and get back to you. After I upgraded to 0.8.1 everything was peachy.
-

Avi Flombaum 2009-03-04 13:19:34 UTC
Yah, I tested this, there is a bug.
class Article < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :articles end # Get all the users that have an article with id > 1 User.find(:all, :joins => :article, :conditions => { :articles => { :id_gt => 1 } }) Returns: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'users.articles' in 'where clause': SELECT 'users'.* FROM 'users' INNER JOIN 'articles' ON 'users'.id = 'articles'.user_id WHERE 'articles'.'id_gt' = 1 The bug is that now ar-extensions is not interpreting the hash'd conditions - it's leaving rails to do it. In the commit that fixed it you said "leave hash conditions alone", so I bet it has to do with not just leaving them alone, but also interpreting them to accept the wonder ar-extension add ons. Keep up the great work Zach, this project is crucial to my site! I'd be happy to help contribute. -

Alan Peabody 2009-03-04 13:19:59 UTC
Whoops, pasted the wrong output.
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'divisions.name_contains' in 'where clause': SELECT
tickets.* FROMticketsINNER JOINdivisionsONdivisions.id =tickets.division_id WHERE (divisions.name_contains= 'hq')Conditions with no joins work perfectly.
-

Avi Flombaum 2009-03-04 13:20:38 UTC
Sorry about the bad formatting above. The last line should read:
Returns: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'users.articles' in 'where clause': SELECT 'users'.* FROM 'users' INNER JOIN 'articles' ON 'users'.id = 'articles'.user_id WHERE 'articles'.'id_gt' = 1
The bug is that now ar-extensions is not interpreting the hash'd conditions - it's leaving rails to do it. In the commit that fixed it you said "leave hash conditions alone", so I bet it has to do with not just leaving them alone, but also interpreting them to accept the wonder ar-extension add ons. Keep up the great work Zach, this project is crucial to my site! I'd be happy to help contribute.
-

Zach Dennis 2009-03-04 13:42:45 UTC
Ah, I see what's going on. This is actually a new feature, since before Rails added hash support for joins I never intended to crawl down a nested hash.
This one will be tricky, because I'll have to update ar-extensions to hook into whatever mechanism Rails uses to crawl the the hash itself.
I don't have the time right now to look into this. If you would like to look into this I'm gladly accept a patch.
Thanks for reporting it,
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
ActiveRecord::Extension (aka ar-extensions) is a plugin to extend and
enhance the functionality of ActiveRecord. It starts by adding better
find support for ActiveRecord. It then adds mass data import
capabilities which are highly efficient and lastly it supports to_csv
functionality.
It also introduces a cool concept of creating easily extendable pieces
of ActiveRecord functionality, so developers don't have to understand
ActiveRecord internals or have the fear of breaking ActiveRecord
itself.