Mathieu Chappuis
2006-Feb-28 14:09 UTC
[Rails] [AR] #{id} namespace visibility used in finder_sql
Dears, [Rails 1.0.0] I''m working with a legacy schema, and around my 20+ models i''ve used some AR constructs. They are based on a finder_sql doing some dirty sql and using #{id} from the ''pivot'' model for extracting data in other tables. like : class Division < ActiveRecord::Base set_table_name "legacy_division" set_primary_key "divid" set_sequence_name "SQ1_DIV" has_many :members, :class_name => ''Division'', # Result is not really a Division class # but that works.. :finder_sql => ''select u.userid, u.username, p.personname, <etc> <snip> where <snip> s.divid = #{id}'' Here #{id} is correctly used as the current Divison record''s id for querying, works as explained in all api examples. m=Division.find(18).members returns a list of users. Now, inside another model, a try to use #{id} inside finder_sql, for using current record and grab external datas using active model record''s id. In console, i got a warning (not present when I''m playing with my other models, as described before) :> warning: Object#id will be deprecated; use Object#object_idSo, it''s like #{id} is not understood by AR to be the record''s id but another "thing" in the object space. And this model''s code is not very different from others : Class Service < ActiveRecord::Base set_table_name "legacy_service" set_primary_key "serid" set_sequence_name "SQ1_SERV" has_many :senders, :class_name => ''Service'', :finder_sql => "select sendername, sendercode from legacy_senders where cid in ( select cid from legacy_mail serid = #{id} ) " end In the log, the query for> s=Service.find(number).sendersgive the objectid (253625) in the SQL, not the the record''id (124) from the Service. I''m very confused with that. Any guideline will be very appreciated Ok, that can be "solved'' using a find_by_sql in my controller, but I feel that soo dirty.. Thanks, Mathieu
Mark Reginald James
2006-Feb-28 19:17 UTC
[Rails] Re: [AR] #{id} namespace visibility used in finder_sql
Mathieu Chappuis wrote:> has_many :senders, > :class_name => ''Service'', > :finder_sql => "select sendername, sendercode from legacy_senders > where cid in ( > select cid from legacy_mail > serid = #{id} > ) " > end > > In the log, the query for > >>s=Service.find(number).senders > > give the objectid (253625) in the SQL, not the the record''id (124) > from the Service.Use single quotes rather than double quotes around your finder_sql in order to delay evaluation. -- We develop, watch us RoR, in numbers too big to ignore.
Jeremy Evans
2006-Mar-01 00:17 UTC
[Rails] Re: [AR] #{id} namespace visibility used in finder_sql
On 2/28/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> Mathieu Chappuis wrote: > > > has_many :senders, > > :class_name => ''Service'', > > :finder_sql => "select sendername, sendercode from legacy_senders > > where cid in ( > > select cid from legacy_mail > > serid = #{id} > > ) " > > end > > > > In the log, the query for > > > >>s=Service.find(number).senders > > > > give the objectid (253625) in the SQL, not the the record''id (124) > > from the Service. > > Use single quotes rather than double quotes around your finder_sql > in order to delay evaluation.Or escape it: :finder_sql => "select sendername, sendercode from legacy_senders where cid in ( select cid from legacy_mail serid = \#{id} ) " Escaping may be better than using single quoted strings, as it allows you to subsitute some values with #{} but not others.