Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Join Table Conditions Broken

#14

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'

Reported by Avi Flombaum · February 2nd, 2009 @ 09:11 PM

State: resolved
Milestone: none
Assigned to: Zach Dennis Zach Dennis

Activity

  1. Avi Flombaum
    Avi Flombaum
    • Assigned user set to Zach Dennis

    February 8th, 2009 @ 04:58 PM

  2. Zach Dennis
    Zach Dennis

    This is fixed in commit a74cc8aa70172507730484edf26ed0bcc2ae1055 . I'll push out a gem soon.

    February 9th, 2009 @ 08:03 PM

  3. Zach Dennis
    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.

    February 9th, 2009 @ 08:13 PM

  4. Alan Peabody
    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.* FROM tickets INNER JOIN divisions ON divisions.id = tickets.division_id INNER JOIN companies ON companies.id = divisions.company_id WHERE (companies.name_contains = 'hq')

    March 4th, 2009 @ 12:40 PM

  5. Avi Flombaum
    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.

    March 4th, 2009 @ 12:57 PM

  6. Avi Flombaum
    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.
    

    March 4th, 2009 @ 01:19 PM

  7. Alan Peabody
    Alan Peabody

    Whoops, pasted the wrong output.

    ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'divisions.name_contains' in 'where clause': SELECT tickets.* FROM tickets INNER JOIN divisions ON divisions.id = tickets.division_id WHERE (divisions.name_contains = 'hq')

    Conditions with no joins work perfectly.

    March 4th, 2009 @ 01:19 PM

  8. Avi Flombaum
    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.

    March 4th, 2009 @ 01:20 PM

  9. Zach Dennis
    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,

    March 4th, 2009 @ 01:42 PM

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.