3

There's a config file config/initializers/inflections.rb that, as per this question lets you modify the puralization of certain items: ruby on rails pluralization help?

However, I'm not interested in that. I want to turn the automatic modification of generated model names off.

Here's an example:

rails generate scaffold UserData data1:string data2:string

Data is changed to 'Datum':

%] cat app/models/user_datum.rb 
class UserDatum < ActiveRecord::Base
  attr_accessible :data1, :data2
end

This is undesirable behaviour.

How do I turn it off?

Specifically if you can please; I've seen a few threads with people saying things like 'you'll have to modify the recipe for that', but no actual guide to doing this.

(I appreciate people are going to want to start answering this with 'you should just stick to the rails way of doing things, there's a good reason for this and it'll work out in the long run'; please dont)

Community
  • 1
  • 1
Doug
  • 32,844
  • 38
  • 166
  • 222

2 Answers2

5

First of all, UserDatum is singular.

In any case:

Change your config/initializers/inflections.rb:

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

(Use whatever naming convention you use, e.g., if you use underscores, user_data instead, or both.)

If you want to remove all pluralizations (sketchy: this will affect everything in the world):

ActiveSupport::Inflector.inflections do |inflect|
  inflect.clear
  inflect.singular(/$/i, '')
end

If you want to control only model/model file naming, patch ModelGenerator:

module Rails
  module Generators
    class ModelGenerator
      def plural_name; singular_name; end
      def plural_file_name; file_name; end
    end
  end
end
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • This doesn't work in all cases. Example 'VAsset' still generates 'v_assets_controller.rb' --> 'class VAssetsController < ApplicationController'. I really want a way to turn this behaviour off _entirely_ not bit-by-bit exceptions (although I'll take bit by bit exception if there's no other option; so long as they work...). – Doug Sep 08 '12 at 13:31
  • @Doug It doesn't work in *any* case except the one(s) you specify; I thought that would be obvious. – Dave Newton Sep 08 '12 at 13:50
  • No, try it. If you put an exception in as inflect.uncountable %w(VAsset) it does _not_ work for VAsset. – Doug Sep 08 '12 at 13:58
0

The best way of doing this is:

rails generate scaffold HouseData --force-plural

(it's still an item by item fix, but it doesn't mess around with the pluralization stuff, which is global and affects other parts of the app too)

Doug
  • 32,844
  • 38
  • 166
  • 222