Hi,
a ticket entry in TRAC (http://dev.rubyonrails.org/ticket/9733) put me onto a small feature that I would like to implement in ActiveRecord but since it involves a change to a private ActiveRecord::Base method I'm not sure how best to go about it.
The change is trivial and verrrry lightly tested but it allows me to put the name of an association into the :condition hash. ActiveRecord recognises it as such and so it allows me to have table names that are different to the association name.
An example:
class FormalProof < AR belongs_to :formal_result end
class FormalResult < AR has_many :formal_proofs end
For legacy reasons the tables are DM19_FORMAL and DM19_FORMAL_PROOFS for FormalResult and FormalProof respectively.
I'd like to write:
FormalProof.find(:first,:conditions => {'formal_result.module_name' => 'module1'}, :include => :formal_result)
and have it emit:
SELECT DM19_FORMALPROOFS.`id` AS t0_r0, ..., FROM DM19_FORMALPROOFS LEFT OUTER JOIN DM19_FORMAL ON DM19_FORMAL.id = DM19_FORMALPROOFS.formal_result_id WHERE (`DM19_FORMAL`.`module_name` = 'module1')
The edge release doesn't know that the :conditions hash refers to an association, it considers it a table.column_name reference and the generated SQL contains the wrong (for me) table name.
Anywaaaay, my hack looks like:
In base.rb, method sanitize_sql_hash_for_conditions
# Extract table name from qualified attribute names. if attr.include?('.') table_name, attr = attr.split('.', 2) - table_name = connection.quote_table_name(table_name) + if reflect_on_association(table_name.to_sym) + table_name = connection.quote_table_name(reflect_on_association (table_name.to_sym).klass.table_name) + else + table_name = connection.quote_table_name(table_name) + end else table_name = quoted_table_name end
which seems to do the right thing for me. I don't think it's too bad a hack either ...
How do I get this into my application?!
Allan