Allan
2007-Oct-17 18:02 UTC
Edge rails: associations in sanitize_sql_hash_for_conditions hack/feature
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2007-Oct-17 19:34 UTC
Re: Edge rails: associations in sanitize_sql_hash_for_conditions hack/feature
On 10/17/07, Allan <Allan.Cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.String conditions are always sql fragments and shouldn''t be conflated with associations, in my opinion. For associations, I would love to see FormalProof.find(:first, :include => :formal_result, :conditions => { :formal_result => { :module_name => ''module1'' } similar to the way deep association :include are specified. See http://dev.rubyonrails.org/ticket/9907 for the beginning of that approach. jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Allan
2007-Oct-17 20:35 UTC
Re: Edge rails: associations in sanitize_sql_hash_for_conditions hack/feature
On Oct 17, 8:34 pm, "Jeremy Kemper" <jer...-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> On 10/17/07, Allan <Allan.Cochr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> String conditions are always sql fragments and shouldn''t be conflated > with associations, in my opinion.Perhaps, but this feature would be useful to me, in particular. Hence my question is really about how best to hack my world or maybe offer a plugin given the very low level nature of the hack.> For associations, I would love to see > FormalProof.find(:first, :include => :formal_result, > :conditions => { :formal_result => { :module_name => ''module1'' } > similar to the way deep association :include are specified. > > See http://dev.rubyonrails.org/ticket/9907for the beginning of that approach.That would indeed be very useful, but until then, can anyone point me in the right direction? Maybe follow the approach here http://rlucas.net/blog/bugfix/ruby_active_record_makes_raw_sql_a_royal_pain.html Thanks, Allan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2007-Oct-17 21:36 UTC
Re: Edge rails: associations in sanitize_sql_hash_for_conditions hack/feature
On 10/17/07, Allan <Allan.Cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That would indeed be very useful, but until then, can anyone point me > in the right direction? Maybe follow the approach here > http://rlucas.net/blog/bugfix/ruby_active_record_makes_raw_sql_a_royal_pain.htmlI was hoping you were volunteering to move ''until then'' closer to ''now'' ;) jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---