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

Anyone? Surely someone else has faced a similar situation?

Thanks, JB

J B wrote:

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

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

:conditions => 'emplasci=#{number}' :conditions => 'number=#{emplasci}'