Showing posts with label self.primary_key. Show all posts
Showing posts with label self.primary_key. Show all posts

Tuesday, 9 April 2013

No identification of primary key on legacy database table

When using Rails against existing databases, it is unlikely that the Rails naming conventions for tables has been used. This means that models are likely to require the table name to be specified.

The old directive set_table_name has been removed from Rails. You now need to use:
self.table_name= :theTableName
However, this appears to have introduced a break, at least in Rails 4. Specifying the table name on a table that has id as the key column doesn't result in Rails identifying the key column, you still need to specify it:
self.primary_key= :id
 This means that whenever you set the table_name, you have to also set the primary key column name.