Ruby on Rails Table Names
Jan 23
Ruby on Rails does something fairly intelligent that I’ve found to come in handy. Let’s say that you have a model named “Dog”, this will map to a table named “dogs”. It’s even smart enough to know that the plural form of “company” is not “companys”, but “companies”. I’ve generally accepted this default behavior and it’s helped me remain consistent when developing applications.
Yet I recently ran into a problem with a table I wanted to name “history” and not “histories”. It just didn’t seem to fit so I learned that the default table name can be easily overridden in the controller. You can simply use the following statement in your class:
class History < ActiveRecord::Base
set_table_name “history”
end
and you’re done! It doesn’t get any easier than that and now I can feel much better about my table naming conventions. For more information check out page 200 in Agile Development with Rails.
