A B
A B

Reputation: 1936

foreign key relation

I have two model class in rails.

class EmployeeDetail < ActiveRecord::Base
  attr_accessible :department_id
  belongs_to :department, :class_name => "Department", :foreign_key => :department_id
end

and

class Department < ActiveRecord::Base
  attr_accessible :name
  validates :name, presence: true, uniqueness: { case_sensitive: false }
end

i have to create a foreign key relation between these two classes. but when i see the employee_details table, department_id is just a integer column without reference to department table. can any one please help to create foreign key relation. thnks

Upvotes: 0

Views: 103

Answers (2)

Ismael
Ismael

Reputation: 16710

There is no need to. Rails do all the work for it.

What you can do is add an index in a migration. Like:

add_index :employee_details, :department_id

Upvotes: 1

quatermain
quatermain

Reputation: 1452

Try add this to Department model:

has_many :employeedetails

Maybe, you don't need

, :class_name => "Department", :foreign_key => :department_id

because Rails do it for you automatic

Upvotes: 0

Related Questions