#14 ✓resolved
Avi Flombaum

Join Table Conditions Broken

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

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

    Avi Flombaum February 8th, 2009 @ 04:58 PM

    • Assigned user set to “Zach Dennis”
  • Zach Dennis

    Zach Dennis February 9th, 2009 @ 08:03 PM

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

  • Zach Dennis

    Zach Dennis February 9th, 2009 @ 08:13 PM

    • 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

    Alan Peabody March 4th, 2009 @ 12:40 PM

    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')

  • Avi Flombaum

    Avi Flombaum March 4th, 2009 @ 12:57 PM

    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

    Avi Flombaum March 4th, 2009 @ 01:19 PM

    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

    Alan Peabody March 4th, 2009 @ 01:19 PM

    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.

  • Avi Flombaum

    Avi Flombaum March 4th, 2009 @ 01:20 PM

    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

    Zach Dennis March 4th, 2009 @ 01:42 PM

    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.

New-ticket Create new ticket

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.

Pages