2009-07-26 50 views
66

我有一個名爲「服裝」的模型,我想成爲單元(一件衣服)。默認情況下,鐵軌說複數是服裝。對或錯,如果複數是「衣服」,我認爲它會更具可讀性。我如何重寫rails命名約定?

如何覆蓋複數命名約定?我可以在模型中正確使用嗎,所以我不必一遍又一遍地做到這一點?這將如何改變路線的處理方式(我正在使用寧靜的建築)?

+10

如何將您的模型更改爲服裝和服裝? – 2009-07-26 17:15:21

+0

相關:http://stackoverflow.com/q/6178900/1505348 – Lucio 2015-06-21 20:12:31

回答

115

我不是RoR專家,但確實找到了possible approach。從引用的網站,你可以添加config/initializers/inflections.rb文件中拐點規則:

# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect| 
    inflect.irregular 'clothing', 'clothes' 
end 
27

爲Rails 2.3.2,也許2+,你需要做的有一點不同:

ActiveSupport::Inflector.inflections do |inflect| 
    inflect.plural /^(ox)$/i, '\1\2en' 
    inflect.singular /^(ox)en/i, '\1' 

    inflect.irregular 'octopus', 'octopi' 

    inflect.uncountable "equipment" 
end 
5

中添加這您environment.rb文件,如果你是想阻止數據庫多元化

ActiveRecord::Base.pluralize_table_names = false 
-1

使用Ruby 2.2.2的Windows或Linux對我來說最好的解決是:

ActiveRecord::Base.pluralize_table_names = false 

class Persona < ActiveRecord::Base 
end 


personas = Persona.all 
personas.each do | personita | 
    print "#{personita.idpersona} #{personita.nombre}\n" 
end 

p Persona.count