Through method problems with custom foreign_keys


#1

A person has a many company_branches and
A company_branch has many people.
This join is represented using a “contacts” table.
I am using a legacy schema so i am forced into using non-standard rails
primary keys. I am using the new through relationship in rails 1.1.

p=Person.find(1)
b=p.company_branches # gives this error:

ActiveRecord::StatementInvalid: OCIError: ORA-00904:
“CONTACTS”.“COMPANY_BRANCH_
ID”: invalid identifier: SELECT company_branches.* FROM contacts,
company_branch
es WHERE (company_branches.branch_id = contacts.company_branch_id AND
contacts.p
erson_person_id = 1)

Notice that “contacts.company_branch_id” does not exist, it should be
“contacts.com_branch_id”

Here are the models:

class Person < ActiveRecord::Base
set_primary_key “person_id”

has_many :contacts,
:foreign_key=>“person_person_id”,
:class_name=>“Contact”

has_many :company_branches,
:through=>:contacts,
:foreign_key=>“com_branch_id”
end

class CompanyBranch < ActiveRecord::Base

set_primary_key “branch_id”

belongs_to :company,
:foreign_key=>“cmpny_company_id”,
:class_name=>“Company”

has_many :contacts,
:foreign_key=>“com_branch_id”,
:class_name=>“Contact”
end

class Contact < ActiveRecord::Base
set_primary_key “contact_id”

belongs_to :company_branch,
:foreign_key=>“com_branch_id”,
:class_name=>“CompanyBranch”

belongs_to :person,
:foreign_key=>“person_person_id”,
:class_name=>“Person”
end

Any idea what is wrong? Any help would be greately appreciated!
Thanks,
Chris


#2

I believe I ran across the same problem when trying a self-referential
has_many :through relationship. I’ve submitted a patch at
http://dev.rubyonrails.org/ticket/4289 that seems to fix it for me.
You may want to give it a shot.