Rails (generate model) is creating a singularized table instead of plural -- the model is also looking for a singular table

I used the command:

$ rails g model Equipment

and rails performed the following:

  invoke  active_record
  create    db/migrate/20160822040448_create_equipment.rb
  create    app/models/equipment.rb
  invoke    test_unit
  create      test/models/equipment_test.rb
  create      test/fixtures/equipment.yml

As you can see, the migration is singular! So I renamed the migration file and the table name as follows:

class CreateEquipments < ActiveRecord::Migration
  def change
    create_table :equipments do |t|
      # ...
    end
  end
end

Now, after running $ rake db:migrate starting Rail's console $ rails c, it errors out saying it cannot find the table when I try to initiate an Equipment:

>> Equipment.new
PG::UndefinedTable: ERROR:  relation "equipment" does not exist
LINE 5:                WHERE a.attrelid = '"equipment"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"equipment"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "equipment" does not exist
LINE 5:                WHERE a.attrelid = '"equipment"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"equipment"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

I was able to patch it up by adding the following line to the model:

    class Equipment < ActiveRecord::Base
        self.table_name = 'equipments'
        # ...
    end

but, although that fixes the issue, I want to know what's causing the problem to begin with.

Final Note: I tried running #pluralize method on the console, and it wouldn't pluralize the string 'Equipment' in there either:

>> 'Equipment'.pluralize
=> "Equipment"
>> 'door'.pluralize
=> "doors"

Upvotes: 4

Views: 1216

Answers (2)

deepak raghuwanshi
deepak raghuwanshi

Reputation: 144

This issue may have arisen because of a manual configuration setting for this particular word, causing Rails to recognize it as both singular and plural. It is advisable to verify and rectify this in the following file:

config/initializers/inflections.rb

Upvotes: 0

Ropeney
Ropeney

Reputation: 1065

Equipment does not have a plural

http://www.learnersdictionary.com/qa/equipments-equipment-noncount-mass-noun-singular-plural

This is expected behaviour for such words.

Upvotes: 8

Related Questions