Forum: Ruby on Rails [AR] #{id} namespace visibility used in finder_sql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F16def8015a7562a285c65a14be48540?d=identicon&s=25 Mathieu Chappuis (Guest)
on 2006-02-28 15:10
(Received via mailing list)
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_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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-28 20:17
(Received via mailing list)
Mathieu Chappuis 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.
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-03-01 01:17
(Received via mailing list)
On 2/28/06, Mark Reginald James <mrj@bigpond.net.au> 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.
F16def8015a7562a285c65a14be48540?d=identicon&s=25 Mathieu Chappuis (Guest)
on 2006-03-01 14:02
(Received via mailing list)
> On 3/1/06, Jeremy Evans <jeremyevans0@gmail.com> wrote:
> On 2/28/06, Mark Reginald James <mrj@bigpond.net.au> wrote:

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

Cheers
This topic is locked and can not be replied to.