Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Inflection Problem

I'm having a problem with generating scaffold for a Regatta. When I run

rails g scaffold Regatta name:string start_date:datetime

I get a model named regattum and a controller called regatta_controller (instead of regattas_controller)

  invoke  active_record
  create    db/migrate/20110609221608_create_regatta.rb
  create    app/models/regattum.rb
  invoke    test_unit
  create      test/unit/regattum_test.rb
  create      test/fixtures/regatta.yml
   route  resources :regatta
  invoke  scaffold_controller
  create    app/controllers/regatta_controller.rb
  invoke    erb
  create      app/views/regatta
  create      app/views/regatta/index.html.erb
  create      app/views/regatta/edit.html.erb
  create      app/views/regatta/show.html.erb
  create      app/views/regatta/new.html.erb
  create      app/views/regatta/_form.html.erb
  invoke    test_unit
  create      test/functional/regatta_controller_test.rb
  invoke    helper
  create      app/helpers/regatta_helper.rb
  invoke      test_unit
  create        test/unit/helpers/regatta_helper_test.rb
  invoke  stylesheets

identical public/stylesheets/scaffold.css

Obviously this is an inflection problem, but whenever I modify /config/initializers/inflections.rb I get an error saying:

The name 'Regatta' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

I have tried everything I can think of to make it work, but I keep getting an error. Any advice on a solution or a workaround would be greatly appreciated!


Update

Here are some of the things I have tried:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'regatta', 'regattas'
end

That didn't change anything so I tried a few of the options below, among others,in varying combinations to no avail:

 inflect.plural 'regatta', 'regattas'
  inflect.singular 'regattas', 'regatta'
  inflect.singular 'regatta', 'regatta'

Update 2

Here is the code I used in inflections.rb once I figured out what I was doing wrong:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'regatta', 'regattas'
  inflect.singular 'regatta', 'regatta'
  inflect.singular 'regattas', 'regatta'
end

Hopefully this will help someone out in the future!

like image 606
akpb Avatar asked Dec 27 '22 19:12

akpb


2 Answers

I recognize this post is old, but I thought I would contribute my findings as well.

I have been integrating rpsec and cucumber into my application after much development (yes; naughty, naughty). I wanted to generate the rspec stubs for my models and get my application unit tested. I am usually able to move an associated migration out of db/migrate/ and run 'bundle exec rails g ' to regenerate the scaffolds and in this case, the rspec stubs. Not this time.

<terminal>

bundle exec rails g scaffold User email:string ... 
      updated_at:datetime roles_mask:integer --trace

      invoke  active_record

The name 'User' is either already used in your application 
      or reserved by Ruby on Rails. Please choose an alternative 
      and run this generator again.

My problem was resolved by temporarily commenting out the 'devise_for' line in my 'config/routes.rb' file

config/routes.rb

#devise_for :users

Bingo. And this isn't the only time that Devise has surprised me with 'black magic' that is not readily apparent. I un-commented the line once my rspec stubs were generated, of course. Now it's off to writing my unit tests!

like image 185
David Vezzani Avatar answered Jan 06 '23 18:01

David Vezzani


Given the error message saying that Regatta is already in use in your application (it's obviously not reserved by Rails), I'm guessing the Regattum model is still in place.

I'd recommend destroying the scaffold and trying again. It sounds like, once a word is already defined in the application, Rails has a built-in protection against changing the inflection. So, this error being thrown is to protect against unexpected behavior in this very situation.

like image 31
Matchu Avatar answered Jan 06 '23 18:01

Matchu