在rails中更改生成模型的复数形式?

nev*_*ame 10 ruby-on-rails

我正在使用此命令:

rails generate model DayOfMonth day:integer
Run Code Online (Sandbox Code Playgroud)

Rails生成模型"DayOfMonth"和表"day_of_months".

我希望它创建表"days_of_month"而不是.

我知道这与Inflector类和初始化文件夹中的inflector.rb有关.

但我不明白如何让这个工作.

我正在使用Rails 3.

有人可以帮助我或者指点我这个教程吗?

谢谢

Nul*_*ion 15

ActiveSupport::Inflector.inflections do |inflect|
 inflect.irregular 'day of month', 'days of month'
end
Run Code Online (Sandbox Code Playgroud)

阅读:http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html


Jes*_*ott 6

您可以只编辑迁移然后添加

Rails 3.2+/4+

class DayOfMonth < ActiveRecord::Base
   self.table_name = "days_of_month"
end
Run Code Online (Sandbox Code Playgroud)

Rails 3

class DayOfMonth < ActiveRecord::Base
  set_table_name "days_of_month"
end
Run Code Online (Sandbox Code Playgroud)

  • 此语法现已更改为'self.table_name ="days_of_month"' (4认同)

Bis*_*lli 6

你必须说一下初始化器'inflections.rb'中复数形式的'day of day'是什么:

ActiveSupport::Inflector.inflections do |inflect|
     inflect.irregular 'day of month', 'days of month'
     inflect.irregular 'day_of_month', 'days_of_month'
end
Run Code Online (Sandbox Code Playgroud)

这对我有用.虽然,在定义与该模型的关联时,我仍然会遇到错误:

has_many :days_of_month
Run Code Online (Sandbox Code Playgroud)