Rails and Databases: How do you do it nicely when things get messy?
Got a question for some of you rails guru's out there. I am developing what will ultimately become a rails3 app, based on a MySQL database. The problem with my app is the way my da开发者_Go百科tabase is set up. In my rails experience thus far, using standard scaffolding to get my models up and running has worked just fine. Each model gets a single unique ID for each record (PK), and as long as the model names match the attribute names in the database table everything goes well.
This time, the database I am building my app around has already been created and each table uses a combination of two or three columns to form the primary keys. Here is my question... how do you set up a model using ActiveRecord to use a combination of primary keys to identify records properly and manipulate model objects the way I did when there was a single PK?
Thanks for the tips
I would recommend continuing to use a single, integer primary key for the primary table and for relationships. You can create one (or more) 'alternate keys' (or unique indexes) to make sure that they remain unique.
Also, try to avoid using data elements as your primary keys. This creates complexities if the data used for the key changes (which seems to happen most on keys that you originally believe would never change).
Having said that, there's a great article on non-standard primary keys: http://roninonrails.blogspot.com/2008/06/using-non-standard-primary-keys-with.html
精彩评论