Rails - Model name ends with S Rails - Model name ends with S ruby-on-rails ruby-on-rails

Rails - Model name ends with S


What's the right way to do this?

I use inflections to document uncountable entities.

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|  inflect.uncountable "ActiveDns"end

Then you get:

$ rails g scaffold_controller ActiveDns      create  app/controllers/active_dns_controller.rb      invoke  erb      create    app/views/active_dns      create    app/views/active_dns/index.html.erb      create    app/views/active_dns/edit.html.erb      create    app/views/active_dns/show.html.erb      create    app/views/active_dns/new.html.erb      create    app/views/active_dns/_form.html.erb      invoke  test_unit      create    test/functional/active_dns_controller_test.rb      invoke  helper      create    app/helpers/active_dns_helper.rb      invoke    test_unit      create      test/unit/helpers/active_dns_helper_test.rb

Is this what you wanted?


I tested with rails-3.2 (I guess it should work with rails-3.x)

Open your config/initializers/inflections.rb and add a rule:

ActiveSupport::Inflector.inflections do |inflect|  inflect.irregular 'dns', 'dnses'end

And generate the controller

rails g scaffold_controller ActiveDns

And add routes to your config/routes.rb file

resources :active_dnses

Then you should see:

$ rake routes   active_dnses GET    /active_dnses(.:format)          active_dnses#index                POST   /active_dnses(.:format)          active_dnses#create new_active_dns GET    /active_dnses/new(.:format)      active_dnses#newedit_active_dns GET    /active_dnses/:id/edit(.:format) active_dnses#edit     active_dns GET    /active_dnses/:id(.:format)      active_dnses#show                PUT    /active_dnses/:id(.:format)      active_dnses#update                DELETE /active_dnses/:id(.:format)      active_dnses#destroy


I want to share about how I solve my problem.

I have an MVC with a name of Pacman under Data_Warehouse.

Where Pacman is a proper noun. (Cannot be Pacmen with an e, but should be Pacmans with an s)

And Data_Warehouse is like a platform name. (Could be anything, in this case its DataWarehouse::Pacman)

So, I have

Models name - models/data_warehouse/pacman.rb

Views name - views/data_warehouse/pacmans/index.slim

Controller name - controller/data_warehouse/pacmans_controller.rb

The problem is, Rails read this path

data_warehouse_pacmans_path

as

data_warehouse_pacmen_path

because of pluralization.

So

I solved this by adding

ActiveSupport::Inflector.inflections do |inflect|   inflect.uncountable %w( pacmans )end

to the inflections.rb file in Rails.

Hope this would helps