Change plural form of generated model in rails?

前端 未结 3 1898
粉色の甜心
粉色の甜心 2021-02-06 01:28

I\'m using this command:

rails generate model DayOfMonth day:integer

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

相关标签:
3条回答
  • 2021-02-06 02:00
    ActiveSupport::Inflector.inflections do |inflect|
     inflect.irregular 'day of month', 'days of month'
    end
    

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

    0 讨论(0)
  • 2021-02-06 02:07

    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
    
    0 讨论(0)
  • 2021-02-06 02:13

    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
    
    0 讨论(0)
提交回复
热议问题