Skip to content

Model generator should print a warning if passed a plural name and automatically convert it #6809

Closed
chancancode opened this Issue Jun 21, 2012 · 7 comments

4 participants

@chancancode
Ruby on Rails member

I'm sure almost every single Rails developer out there have made this mistake at some point:

$ rails g model posts title:string
      invoke  active_record
      create    db/migrate/20120621124605_create_posts.rb
      create    app/models/posts.rb
      invoke    test_unit
      create      test/unit/posts_test.rb
      create      test/fixtures/posts.yml

If you didn't notice the problem, you may skip the rest of this. Simply go ahead and :+1: this ;)

"Obviously the name of a model should be singular! Everyone knows that."

Maybe. (Personally speaking, I can never remember what the generators expect.) But people make mistakes (case in point - #6746). I volunteered for RailsBridge and introduced plenty of new developers to Rails, and based on what I've seen this is not an uncommon cause of confusion and frustration for beginners. ("Won't somebody please think of the newbies?")

If you are still not convinced, our @tenderlove has famously made this mistake and was caught on camera :P

LOL

Since we are already auto-correcting this for resource and scaffold generators, I don't see why we shouldn't do this on the model generator too.

@chancancode
Ruby on Rails member

I was going to submit this as a PR, but after some digging around I realized the problem is more complicated than I thought:

  1. Previously, String#singularize is not idempotent. The classic example is 'address' => 'addres'. This bug was fixed in #4719, so this should no longer be a problem.

  2. I'm not sure how NamedBase is supposed to work. From what I understand, the "name" parameter is assumed to be singular. I traced all the way back to https://github.com/rails/rails/blob/83f7fe2028df15e058205c93308c0e19abd7157a/railties/lib/generator/named_base.rb#L56-57, and it appears that the plural name is always pluralized explicitly while the singular name is more or less directly taken from the name parameter. (I am guessing this might have something to do with the bug I mentioned earlier. @josevalim?) This seems wrong to me as it is clearly not how it is being used now (e.g. the resource generator passes in a plural name here when creating the controllers). Even if it's not wrong the naming makes it extremely confusing.

  3. I'm not sure where I should implement the check. (NamedBase is probably not a good place to put that logic as it will probably break a lot of generators out there.) Perhaps I should move this to railties/lib/rails/generators/rails/model/model_generator.rb? Would that work?

I really want this bug fixed (if this get fixed, it'll be my favourite new feature in Rails 4!), and if someone can clarify things for me / provide some suggestions, I'm willing to spend some time coming up with a solution.

@steveklabnik
Ruby on Rails member

I really want this bug fixed (if this get fixed, it'll be my favourite new feature in Rails 4!), and if someone can clarify things for me / provide some suggestions, I'm willing to spend some time coming up with a solution.

You will probably get better feedback from the rails core mailing list, honestly.

@andmej
andmej commented Jun 22, 2012

Sounds like a great idea if you can get it working.

@chancancode
Ruby on Rails member

I posted this to the core mailing list per @steveklabnik's suggestion - link here https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/X9Ww8eLM_t4.

@schneems
Ruby on Rails member

:+1: on this if pending reasonable implementation. You mentioned that we're already doing this check and fix in the scaffold generator, how are we doing it there?

In your post you also had some implementation questions, if you can get a working solution and do a pull request cc me and we can talk about implementation.

@chancancode
Ruby on Rails member
@steveklabnik
Ruby on Rails member

Since this is a feature request, I'm giving it a close. @chancancode , please submit a pull.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.