'Rails Conventions pluralisations

I'm new to rails and am struggling a bit with the naming conventions.

I am making an asset tracking software so I have a done

rails g scaffold Location name:string group:string occupent:string

I then want to make a Staff model so

rails g scaffold Staff name:string

But this creates a table called Staffs and a model for Staffs which I think is wrong. I will be doing the same for hardware and software which in turn comes out as hardwares and softwares which is incorrect.

Is it better to think of a different model Name or is there away around this?



Solution 1:[1]

This question is fairly old but I stumbled across it looking for a way to handle the word staff(as in employees at a company) correctly.

The reason it pluralises with the 's' is because the plural of staff(as in wizards staff) is staffs(staves is also valid). So if you needed a table with different kinds of (wizards)staffs it is valid. However the plural of staff(as in people at a company) is staff.

Rails assumes that you are referring to the wizards staff in this instance. With words with only one meaning that don't have a separate plural, this works correctly:

[1] pry(main)> "sheep".pluralize == "sheep" 
=> true

How to fix this?

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural "staff", "staff"
end

This will tell rails that the plural of staff is staff and the naming conventions will be respected.

rails generate scaffold staff name:string description:text active:boolean

      invoke  active_record
      create    db/migrate/20220518125452_create_staff.rb
      create    app/models/staff.rb
      invoke    test_unit
      create      test/unit/staff_test.rb
      create      test/fixtures/staff.yml
      invoke  resource_route
       route    resources :staff
      invoke  inherited_resources_controller
      create    app/controllers/staff_controller.rb
      invoke    erb
      create      app/views/staff
      create      app/views/staff/index.html.erb
      create      app/views/staff/edit.html.erb
      create      app/views/staff/show.html.erb
      create      app/views/staff/new.html.erb
      create      app/views/staff/_form.html.erb
      invoke    test_unit
      create      test/functional/staff_controller_test.rb
      invoke    helper
      create      app/helpers/staff_helper.rb
      invoke      test_unit
      create        test/unit/helpers/staff_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/staff.js.coffee
      invoke    scss
      create      app/assets/stylesheets/staff.css.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.css.scss

The migration:

create_table :staff do |t|
  t.string :name
  t.text :description
  t.boolean :active

  t.timestamps
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1