Join Table Conditions Broken
-
Avi Flombaum
- Assigned user set to Zach Dennis
-
Zach Dennis
This is fixed in commit a74cc8aa70172507730484edf26ed0bcc2ae1055 . I'll push out a gem soon.
-
Zach Dennis
- 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
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
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
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
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
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
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.