我使用命令:$ rails g model Equipment
和rails执行了以下操作:
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
正如你所看到的,迁移是单一的!因此,我将迁移文件和表名重命名为:
class CreateEquipments < ActiveRecord::Migration
def change
create_table :equipments do |t|
# ...
end
end
end
现在,在运行$ rake db:migrate
启动Rail的控制台$ rails c
后,当我尝试启动一个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
我可以通过在模型中添加以下行来修补它:
class Equipment < ActiveRecord::Base
self.table_name = 'equipments'
# ...
end
但是,虽然这解决了问题,我想知道是什么导致的问题开始。
最后一点:我试着在控制台上运行#pluralize方法,它也不会对字符串'Equipment'进行复数化:
>> 'Equipment'.pluralize
=> "Equipment"
>> 'door'.pluralize
=> "doors"
2条答案
按热度按时间ozxc1zmp1#
设备没有复数
http://www.learnersdictionary.com/qa/equipments-equipment-noncount-mass-noun-singular-plural
这是对此类单词的预期行为。
93ze6v8z2#
这个问题可能是因为这个特定单词的手动配置设置导致Rails将其识别为单数和复数。建议在以下文件中对此进行验证和纠正:
config/initializers/inflections.rb