# \[AR\] #{id} namespace visibility used in finder\_sql

**URL:** <https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279>\
**Category:** Rails\
**Created:** [February 28, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279 "2006-02-28T15:10:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathieu\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mathieu\_C](https://www.ruby-forum.com/u/Mathieu_C)\
**Post date:** [February 28, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279/1 "2006-02-28T15:10:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mathieu\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mathieu\_C](https://www.ruby-forum.com/u/Mathieu_C)\
**Post date:** [February 28, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279/2 "2006-02-28T20:17:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mathieu\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mathieu\_C](https://www.ruby-forum.com/u/Mathieu_C)\
**Post date:** [March 1, 2006, 1:17am UTC](https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279/3 "2006-03-01T01:17:37Z")

</div>

On 2/28/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![Mathieu\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mathieu\_C](https://www.ruby-forum.com/u/Mathieu_C)\
**Post date:** [March 1, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/ar-id-namespace-visibility-used-in-finder-sql/54279/4 "2006-03-01T14:02:17Z")

</div>

> On 3/1/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> On 2/28/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Cheers
