0

I am attempting to seed my database but I receive the following error:

NoMethodError: undefined method `create' for Gem:Module

This is very strange because everything looks exactly the same from every other app I made. I can't find anything wrong in terms of naming. Why can Rails not identify Gem as a valid Model object?

app/models/gem.rb

class Gem < ActiveRecord::Base
end

db/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

db/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',
})

Edit:

If I change my model naming to be plural then I can seed correctly:

app/models/gems.rb

class Gems < ActiveRecord::Base
end

But then that requires my referring to Gems (plural) instead of Gem in my seed file.

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',
})

Why is this happending?

EDIT2:

Rails console seems to respond to both Gem and 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 >

1 Answer 1

1

Gem is a reserved word in ruby for ruby gems.

Sign up to request clarification or add additional context in comments.

1 Comment

I just figured that out and renamed my class to 'Mineral'. Works like a charm now. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.