我正在尝试为我的数据库播种,但收到以下错误:
NoMethodError: undefined method `create' for Gem:Module
这很奇怪,因为从我制作的其他应用程序看,一切看起来都一模一样。我在命名方面找不到任何错误。为什么Rails不能将Gem
识别为有效的Model对象?
app / models / gem.rb
class Gem < ActiveRecord::Base
end
分贝/ schema.rb
ActiveRecord::Schema.define(version: 20150119005900) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "gems", force: true do |t|
t.string "name", null: false
t.text "description", null: false
t.float "hardness", null: false
t.string "transparency", null: false
t.string "uri", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
分贝/ seeds.rb
Gem.create({
name: 'Copper',
description: 'Copper is known for its metallic reddish-brown color. Though not a gemstone or precious metal, it is included here in this guide for its historical significance as an ancient metal. Ornaments, coins, and statues have been fashioned from Copper since ancient times. Its distinct color and availability throughout history have afforded it great significance, though in modern times Copper is almost exclusively an industrial metal.',
hardness: 2.5,
transparency: 'Opaque',
uri: 'http://www.minerals.net/GemStoneInTheRoughImages/native-copper-ray-mine.jpg'
})
Gem.create({
name: 'Ruby',
description: 'Ruby is distinguished for its bright red color, being the most famed and fabled red gemstone. Beside for its bright color, it is a most desirable gem due to its hardness, durability, luster, and rarity. Transparent rubies of large sizes are even rarer than Diamonds. Ruby is the red variety of the mineral Corundum. Sapphire, the other gem variety of Corundum, encompasses all colors of Corundum aside from red. In essence, Ruby is a red Sapphire, since Ruby and Sapphire are identical in all properties except for color.',
hardness: 9.0,
transparency: 'Transparent to opaque',
uri: 'http://www.minerals.net/GemStoneImages/ruby-winza-tanzania-g.jpg'
})
Gem.create({
name: 'Emerald',
description: 'Emerald, the green variety of Beryl, is the most famous and valuable green gemstone. Its beautiful green color, combined with durability and rarity, make it one of the most expensive gemstones. Deep green is the most desired color in Emeralds. In general the paler the color of an Emerald, the lesser its value.',
hardness: 7.5,
transparency: 'Transparent to translucent',
uri: 'http://www.minerals.net/GemStoneImages/emerald-gem-241324b.jpg',
})
编辑:
如果我将模型命名更改为复数,那么我可以正确播种:
应用/模型/ gems.rb
class Gems < ActiveRecord::Base
end
但是那需要我在种子文件中引用Gems
(复数)而不是Gem
。
Gems.create({
name: 'Copper',
description: 'Copper is known for its metallic reddish-brown color. Though not a gemstone or precious metal, it is included here in this guide for its historical significance as an ancient metal. Ornaments, coins, and statues have been fashioned from Copper since ancient times. Its distinct color and availability throughout history have afforded it great significance, though in modern times Copper is almost exclusively an industrial metal.',
hardness: 2.5,
transparency: 'Opaque',
uri: 'http://www.minerals.net/GemStoneInTheRoughImages/native-copper-ray-mine.jpg'
})
Gems.create({
name: 'Ruby',
description: 'Ruby is distinguished for its bright red color, being the most famed and fabled red gemstone. Beside for its bright color, it is a most desirable gem due to its hardness, durability, luster, and rarity. Transparent rubies of large sizes are even rarer than Diamonds. Ruby is the red variety of the mineral Corundum. Sapphire, the other gem variety of Corundum, encompasses all colors of Corundum aside from red. In essence, Ruby is a red Sapphire, since Ruby and Sapphire are identical in all properties except for color.',
hardness: 9.0,
transparency: 'Transparent to opaque',
uri: 'http://www.minerals.net/GemStoneImages/ruby-winza-tanzania-g.jpg'
})
Gems.create({
name: 'Emerald',
description: 'Emerald, the green variety of Beryl, is the most famous and valuable green gemstone. Its beautiful green color, combined with durability and rarity, make it one of the most expensive gemstones. Deep green is the most desired color in Emeralds. In general the paler the color of an Emerald, the lesser its value.',
hardness: 7.5,
transparency: 'Transparent to translucent',
uri: 'http://www.minerals.net/GemStoneImages/emerald-gem-241324b.jpg',
})
为什么会这样?
EDIT2:
Rails控制台似乎响应Gem
和Gems
Loading development environment (Rails 4.1.6)
2.1.2 :001 > Gem
=> Gem
2.1.2 :002 > Gems
=> Gems (call 'Gems.connection' to establish a connection)
2.1.2 :003 >
答案 0 :(得分:1)
宝石是红宝石中红宝石的保留词。