I'm trying to migrate a desktop application to rails (also dealing with quite old fashioned existing database). The problem is that I don't have a unique ID in one column, but it's three columns of a table that guarantee uniqueness of a record.
Given I have 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 consists of author_letter, author_nr1, author_nr2 here.
So do I need sort of a multicolumn primary key here to have rails associations working? Or am I going in the wrong direction here?
As many people said: "if you fight Rails, it'll strike back". Really try to avoid such situations, It's pain with rails, if you don't have a clean datamodel.
There exists a gem called composite_primary_keys that will allow to build a primary key using multiple columns.
So, yes, you can use multicolumn primary key.
But, if you are able to change the datamodel (which is not always the case), I would propose to add a column ID to each table, as this will make your life easier (and is also much more performant).
[EDIT]
Your class definition with composite_primary_keys will look like this
Hope this helps.
No. The Primary Key is (like rails default) the ID of the Record.
In addition you can set unique Keys like
This potects your database. To catch the uniqueness in Rails you need to write into your model: