I'm working with a legacy MSSQL database with non-traditional table names and foreign keys. For our purposes tablel 1 is named 'tblFoo" and table 2 is named "tblBar". I named the models for the before mentioned tables foo.rb and bar.rb, using set_table and set_primary_id. Foo has_many :bars and bars belongs_to :foo :foreign_key => 'fldBarID
In the process of performing a find with conditions on the two tables I produced the following code:
@Foos = Foo.all(:include => :bars, :conditions => {:id => '25', :bars => { :created_on => '11/06/2002'}}) or @Foos = Foo.all(:joins => :bars, :conditions => {:id => '25', :bars => { :created_on => '11/06/2002'}})
...and the above code produces the following error:
ActiveRecord::StatementInvalid: DBI::DatabaseError: 37000 (4104) [unixODBC][FreeTDS][SQL Server]The multi-part identifier "bars.created_on" could not be bound. The error is caused when rails does not replace the :bars symbol with the correct table name (:tblBar) in WHERE section the generated sql. (but the table name is correctly produced in the INNER JOIN section).
However.... if I format my find as follows, its all good (note the symbol tblBar in the conditions with is the literal table name)
@Foos = Foo.all(:include => :bars, :conditions => {:id => '25', :tblBar => { :created_on => '11/06/2002'}}) or @Foos = Foo.all(:joins => :bars, :conditions => {:id => '25', :tblBar => { :created_on => '11/06/2002'}})
Naming the models after the tables like tbl_foo.rb produces the same error describe above and so does creating the has_many association:
has_many :tblBar, :class => 'bar'
Is this a bug in the find or the MSSQL driver?
Just for reference: http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions http://github.com/rails/rails/commit/cd994eff9a343df376bfaec59de5b24a2ab51256