Hello, can anyone please tell me if what i want to do is ok to do with
Rails, or will there arise some serious obstacle?
If it is ok to do, then i will probably figure out a way after some
trial and error.
Any other suggestion will also be appreciated.
I want to share the primary key among several tables, and then to also
use it as the foreign key for has_one associations.
I have models “Person”, “Instructor”, and “Member” (tables “people”,
“instructors”, and “members”).
Instructors and members of an association are people, so i plan to store
their personal information in “people” table, and to store a foreign key
“person_id” in “instructors” and “members” tables.
I also plan to have the associations:
class Person < ActiveRecord::Base
has_one :instructor, :dependent => :destroy
has_one :member, :dependent => :destroy
end
class Instructor < ActiveRecord::Base
belongs_to :person
end
class Member < ActiveRecord::Base
belongs_to :person
end
It is possible for the same person to be an instructor and a member in
the
same time, or neither of two.
Now i do not need “id” in “instructors” and “members” tables, as i can
use “person_id” as the primary key.
I plan to create the tables with migrations like these:
class CreateInstructors < ActiveRecord::Migration
def self.up
create_table :instructors, :primary_key => “person_id” do |t|
t.text :presentation
t.binary :photo
…
end
end
…
end
…
class CreateMembers < ActiveRecord::Migration
def self.up
create_table :members, :primary_key => “person_id” do |t|
t.date :member_since
…
end
end
…
end
My question is: is this going to work, or will i encounter some
unresolvable issues?
(For example, when creating a new Instructor, how to specify its primary
key “person_id” to be equal to the “id” of an existing Person?)
Thanks.
Alexey.