I am attempting to migrate a desktop application to rails (also coping with quite traditional existing database). However , I do not possess a unique ID in a single column, but it is three posts of the table that guarantee originality of the record.
Given I've three tables:
authors author_name, author_letter, author_nr1, author_nr2 ... titles titel_nr, titel_name, ... author_titles titel_nr, author_letter, author_nr1, author_nr2
The "primary key" of authors includes author_letter, author_nr1, author_nr2 here.
So I want kind of a multicolumn primary key here to possess rails associations working? Or shall we be held pointed in the wrong direction here?
unsure basically got your question right:
no. The Main Secret is (like rails default) the ID from the Record.
additionally you are able to set unique Secrets like
add_index :users, [:merchant_id, :email], :unique => true add_index :users, [:merchant_id, :login], :unique => true
this potects your database. to trap this in rails you have to write inside your model:
validates_uniqueness_of :email, :scope => :merchant_id validates_uniqueness_of :login, :scope => :merchant_id
There is available a jewel known as composite_primary_keys that will permit to construct a principal key using multiple posts.
So, yes, you should use multicolumn primary key.
But, if you can to alter the datamodel (which isn't always the situation), I'd offer give a column ID to every table, as this makes your existence simpler (and it is a lot more performant).
Your class definition with composite_primary_secrets may be like this
class Author set_primary_keys :author_letter, :author_nr1, :author_nr2 has_many :titles, :through => :author_title end class Title set_primary_key :title_nr end class AuthorTitle belongs_to :title, :foreign_key => :title_nr belongs_to :authori, :foreign_key => [:author_letter, :author_nr1, :author_nr2] end
Hope this can help.