views:

280

answers:

1

Not sure what the difference between underscore (student_state) and tableize (student_states), other than tableize also pluralizes. However, not sure how they can be used differently. Obviously, you can use tableize to reference table name in database. But what different functionality does underscore provide, such as when you see :student_state compared to :student_states when used as symbols. Thanks for suggestions.

+3  A: 

tableize will pluralize your string whether the original was singular or plural, while underscore will only add underlines.

While this may seem trivial, it's all about abstracting the details of database implementation away from the developer. If, down the road, Rails began formatting table names differently, the only method that would need to change would be tableize. All other places in the Rails code that refer to table names can stay the same, because they're still calling the tableize method. A change to the underlying structure of rails is therefore limited and much less damaging.

This is referred to as "orthoganality" in computer science. Now that you know what it means, try to throw it around in conversation to make yourself look smarter. Did it work for me? :)

Jaime Bellmyer
Is there any different functionality? For example, table names should be plural. The table will map to a Model object that has the same name but in the singular form. Is there a reason to call something pluralized and something else singularized at any time?
JohnMerlino
For example, if you had a model named StudentState, and you used underscore Student_State and then tableize Student_States. Do you use the former to call a method and the latter to retrieve from database?
JohnMerlino
Actually, it would be "student_states" lowercase. The underscore method both lowercases and adds underscores, because it's actually the opposite of camelize, which will convert an underscored string to "camelcase". If you're new to rails, you don't need to be concerned with these methods, because they're used for metaprogramming. For example, if you're writing a plugin you might need to convert a class name to a table name, and so forth.
Jaime Bellmyer