It this possible: finder_sql-like behavior for belongs_to?

Guys,

I have a need to support as has_many/belongs_to relationship on a
legacy(kind of) schema. The reason I say kind of is that the schema does
have “id” columns that are used in many associations, but this
particular
has_many/belongs_to association needs to support different ones.

I’ve attached to code at the end of this email. Suffice it to say I need
to
use the standard “id” columns in most associations within the
application,
but for this particular MajicEmployee to GateClockEmployee relationship,
I
need to use two non-standard columns for joining.

The :finder_sql option solves this problem for me on the has_many side,
but
belongs_to doesn’t seem to support :finder_sql (although I’ve included
it in
the below code to illustrate what I’m trying to do).

Can anyone tell me how I can get the belongs_to side working so I can
query
from that side as well?

Any help will be GREATLY appreciated.

Thanks!
JB

class MajicEmployee < ActiveRecord::Base
has_many :majic_entries
has_many :gate_clock_employees,
:finder_sql=>‘select * from gate_clock_employees where
emplasci=#{number}’
end

class GateClockEmployee < ActiveRecord::Base
has_many :gate_clock_entries
belongs_to :majic_employee, :finder_sql=>‘select * from
majic_employees
where number=#{emplasci}’
end

gateclock=# \d gate_clock_employees
Table "public.gate_clock_employees "
Column | Type |
Modifiers
------------±-----------------------±------------------------------------------------------------------
id | integer | not null default
nextval(‘gate_clock_employees_id_seq’::regclass)
tagnum | integer |
pin | character varying(255) |
lastname | character varying(255) |
firstname | character varying(255) |
mi | character varying(255) |
dept | character varying(255) |
title | character varying(255) |
tel_ext | character varying(255) |
employe_no | integer |
parking_id | integer |
vehicle_1 | character varying(255) |
vehicle_2 | character varying(255) |
user_1 | text |
user_2 | text |
user_3 | text |
user_4 | text |
user_5 | text |
user_6 | text |
user_7 | text |
usernum | integer |
usrgrp_id | character varying(255) |
location | character varying(255) |
u_photo | character varying(255) |
emplasci | integer |

gateclock=# \d majic_employees
Table “public.majic_employees”
Column | Type | Modifiers
--------±--------±-------------------------------------------------------
id | integer | not null default
nextval(‘employees_id_seq’::regclass)
number | integer |

J B wrote:

The :finder_sql option solves this problem for me on the has_many side,

majic_employees where number=#{emplasci}’
end

Try replacing the finder_sql for both associations
with a :conditions option

:conditions => ‘emplasci=#{number}’
:conditions => ‘number=#{emplasci}’


We develop, watch us RoR, in numbers too big to ignore.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs