1

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?

Greyhounddad
  • 115
  • 3
  • 9
  • Possible duplicate of [How do I override rails naming conventions?](http://stackoverflow.com/questions/1185035/how-do-i-override-rails-naming-conventions) – Martin Schneider Feb 02 '16 at 09:23
  • but is it best to override it ? will I run into problems further along if I make it Staff and Staff not Staff and Staffs? – Greyhounddad Feb 02 '16 at 09:25
  • I don't know since i've never been on that path. But - personally - I would not override it since it "feels" like I am fighting the framework conventions. – Martin Schneider Feb 02 '16 at 09:33
  • The model name is always singular and the convention is for the table to by plural. Why do you think it is incorrect? – BroiSatse Feb 02 '16 at 09:40
  • But the plural of staff is staff not staffs. same with hardware and software the plural is hardware and software. – Greyhounddad Feb 02 '16 at 11:52
  • To add to this. There are two possible pluralisations of staff. Staff as in members of staff at a company isn't pluralisable. Staff as in a wizards staff is pluralisable. The company has many staff(not staffs). The evil wizard grabbed both the staffs(staves is also valid)(not staff). – DickieBoy May 18 '22 at 12:44

2 Answers2

0

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

Edit:

ActiveSupport also has uncountable. Thats means to handle things like sheep and fish.

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( staff )
end
DickieBoy
  • 4,886
  • 1
  • 28
  • 47
-3

The commands which you are trying are correct and i don't see a need to override anything.

  rails g scaffold Staff name:string
  invoke  active_record
  create    db/migrate/20160202100158_create_staffs.rb
  create    app/models/staff.rb
  invoke    test_unit
  create      test/models/staff_test.rb
  create      test/fixtures/staffs.yml
  invoke  resource_route
   route    resources :staffs
  invoke  scaffold_controller
  create    app/controllers/staffs_controller.rb
  invoke    erb
  create      app/views/staffs
  create      app/views/staffs/index.html.erb
  create      app/views/staffs/edit.html.erb
  create      app/views/staffs/show.html.erb
  create      app/views/staffs/new.html.erb
  create      app/views/staffs/_form.html.erb
  invoke    test_unit
  create      test/controllers/staffs_controller_test.rb
  invoke    helper
  create      app/helpers/staffs_helper.rb
  invoke      test_unit
  invoke    jbuilder
  create      app/views/staffs/index.json.jbuilder
  create      app/views/staffs/show.json.jbuilder
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/staffs.coffee
  invoke    scss
  create      app/assets/stylesheets/staffs.scss
  invoke  scss
  create    app/assets/stylesheets/scaffolds.scss

This scaffold command will create a full fledged mvc structure. Model name is Staff which is correct as per conventions.

If you specifically want to create controllers

  rails g controller controller_name

or for creating models

  rails g model model_name.

I would suggest to have a look over this Rails scaffold description

Bijendra
  • 9,467
  • 8
  • 39
  • 66