Primary Key used as foreign key in another table

244 Views Asked by At

In rails 2, how can we find out that certain primary key in one table is used as foreign key in another table. Like I have a table punching_informations having 'id' as primary key. I want to find out that whether an 'id' from punching_informations table is used as foreign key in another table named punching_request. PunchingInformation has one PunchingRequest.

1

There are 1 best solutions below

0
Salil On BEST ANSWER

Ideally there should be a punching_information_id in your punching_request table. In that case your punching_request.rb model will have following line.

belongs_to :punching_information

However you can change this by using the element :foreign_key like following

belongs_to :punching_information, foreign_key: "some_new_id"