Forum: Ruby on Rails ActiveRecords - two fks

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.
1ca5a9ef6be05c1ccc4cdc631162bc1e?d=identicon&s=25 Zirael (Guest)
on 2007-03-19 11:43
(Received via mailing list)
Hi,

     I have two database tables
     Briefly:
       Projects
          -type
          -designer_id
          -check_designer_id
          - ...
       Designers
          -name
          -...

     I want to create two foreign keys in Projects table and both with
     the same Designer table.

     How should I deal with that problem, I want to use ActiveRecords.
     Is it possible?
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2007-03-19 18:34
Zirael wrote:
> Hi,
>
>      I have two database tables
>      Briefly:
>        Projects
>           -type
>           -designer_id
>           -check_designer_id
>           - ...
>        Designers
>           -name
>           -...
>
>      I want to create two foreign keys in Projects table and both with
>      the same Designer table.
>
>      How should I deal with that problem, I want to use ActiveRecords.
>      Is it possible?

Yes,  Serving from memory (at work taking a break),  look at belongs_to
:foreign_key to specify alternate/extra foreign key relationships..

class Project < ActiveRecord::Base
    belongs_to :designer, :foreign_key=>"designer_id"
    belongs_to :designer, :foreign_key=>"some_other_foreign_key_id"
end

class Designer < ActiveRecordBase
    has_many :projects, :foreign_key=>"designer_id"
    has_many :projects, :foreign_key=>"some_other_foreign_key_id"
end

Designer.find(:first).some_other_foreign_keys.each { etc..... }

hope this helps

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