Forum: Ruby on Rails Through method problems with custom foreign_keys

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.
Chris (Guest)
on 2006-03-15 13:25
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
Lee M. (Guest)
on 2006-03-18 00:15
(Received via mailing list)
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.
This topic is locked and can not be replied to.