Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change plural form of generated model in rails?

I'm using this command:

rails generate model DayOfMonth day:integer

Rails generated the model "DayOfMonth" and the table "day_of_months".

I want it to create the table "days_of_month" instead.

I know this has something to do with the Inflector class and the inflector.rb in initializers folder.

But I don't understand how to get this to work.

I'm using Rails 3.

Could someone help me out here or point me to a tutorial for this?

Thanks

like image 329
never_had_a_name Avatar asked Jul 31 '10 13:07

never_had_a_name


3 Answers

ActiveSupport::Inflector.inflections do |inflect|
 inflect.irregular 'day of month', 'days of month'
end

Read: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html

like image 87
NullUserException Avatar answered Nov 14 '22 03:11

NullUserException


You could just edit the migration and then add

Rails 3.2+ / 4+

class DayOfMonth < ActiveRecord::Base
   self.table_name = "days_of_month"
end

Rails 3

class DayOfMonth < ActiveRecord::Base
  set_table_name "days_of_month"
end
like image 31
Jesse Wolgamott Avatar answered Nov 14 '22 02:11

Jesse Wolgamott


You have to say what's the plural form of 'day of month' in an initializer 'inflections.rb':

ActiveSupport::Inflector.inflections do |inflect|
     inflect.irregular 'day of month', 'days of month'
     inflect.irregular 'day_of_month', 'days_of_month'
end

That worked for me. Although, I'm still getting errors when defining associations to that model:

has_many :days_of_month
like image 6
Bishma Stornelli Avatar answered Nov 14 '22 01:11

Bishma Stornelli