[AR] #{id} namespace visibility used in finder_sql


#1

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,

where

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_id

So, 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).senders

give 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


#2

Mathieu C. wrote:

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.


#3

On 2/28/06, Mark Reginald J. removed_email_address@domain.invalid wrote:

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.


#4

On 3/1/06, Jeremy E. removed_email_address@domain.invalid wrote:
On 2/28/06, Mark Reginald J. removed_email_address@domain.invalid wrote:

Thanks a lot for pointing me that good way. It works

Cheers