Sharing primary key between two tables?

Hello,

I have an ActiveRecord question.
I have two tables, “customers” and “users”. Those two tables really
should share their primary key, in order to have a “real” 1:1
relationship.

(I don’t mean to start a database-resign religious war, but I must say
that I do feel very uneasy about using ActiveRecord’s “normal” way,
which is to stick a foreign key into one of the two tables. That does
NOT look like a 1:1 or 1:0 relationship to me!)

With ActiveRecords I can then use belongs_to this way:

class User < ActiveRecord::Base
has_one :customer, :foreign_key => “id”
end

class Customer < ActiveRecord::Base
has_one :user, :foreign_key => “id”
end

This should actually work. However, I read all over the internet (AND in
the agile book) scary messages about not allowing ActiveRecords having
an auto_increment primary key. In my case, I would have an
auto_increment for “users”, which means that every time I create a
customer, I will also need to create a user forst and use THAT primary
key for the customer.

I am using Postgresql. Now… am I gonna run into endless, weird
problems if I disable the auto_increment for customers? Or is it safe
for me to do so? Is ActiveRecords gonna allow me to pass the :id when I
create a record without any dramas? Is this “documented/safe”?

Thanks a lot,

Merc.

Hi,

Here:

class User < ActiveRecord::Base
has_one :customer, :foreign_key => “id”
end

class Customer < ActiveRecord::Base
has_one :user, :foreign_key => “id”
end

I actualy meant:

class User < ActiveRecord::Base
belongs_to :customer, :foreign_key => “id”
end

class Customer < ActiveRecord::Base
belongs_to :user, :foreign_key => “id”
end

Bye,

Merc.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs