6.2. Naming Unconventionally

ActiveRecord has a few useful class-level attributes that enable you to change the default relationship between table names and class names, and the default primary key names. You can't use them in this case, because they are class-level, and would apply to all ActiveRecord classes in the project, even the ones that have already been created against the traditional Rails database. However, it's worth listing them here (which I've done in the following table) because under somewhat different project parameters, they are very useful. Typically, you'd put these changes in your environent.rb file.

Attribute Description
pluralize_tablenamesDefaults to true. If your database schema uses the singular form of the table name instead of the plural, set this value to false.
primary_key_prefix_typeSpecifies common behavior for primary key names where the table name is used as part of the key. If the key names are of the form recipeid, this field should have the value :table_name. If the key names are of the form recipe_id, this field should have the value :table_name_with_underscore. The default is nil.
table_name_prefixA common string at the beginning of each table name. For example, you could use the_.
table_name_suffixA common string at the end of each table name. For example, you could use _table.

Because you can't use these defaults, the new IngredientData class needs to specify the table name and primary key name explicitly. Create an app/models/ingredient_data.rb ...

Get Professional Ruby on Rails™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.