# Undefined method \`create'

**URL:** <https://www.ruby-forum.com/t/undefined-method-create/123936>\
**Category:** Ruby\
**Created:** [December 22, 2007, 9:13am UTC](https://www.ruby-forum.com/t/undefined-method-create/123936 "2007-12-22T09:13:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 22, 2007, 9:13am UTC](https://www.ruby-forum.com/t/undefined-method-create/123936/1 "2007-12-22T09:13:23Z")

</div>

I want to create one creature and write it to the dwemthys.db, and I’m  
close. From the example at \<[http://www.dzone.com/links/rss/](http://www.dzone.com/links/rss/)  
simple\_ruby\_activerecord\_example.html?\> I know that the “create” method  
is used for this, but somehow it fails when for creatures when it works  
for the example.

All code is at [http://code.google.com/p/dwemthys/source](http://code.google.com/p/dwemthys/source), but this is  
what I’m running into:

thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ ruby creatures.rb  
– create\_table(:creatures)  
SQL (0.016574) CREATE TABLE creatures (“id” INTEGER PRIMARY KEY NOT  
NULL, “life” integer DEFAULT NULL, “strength” integer DEFAULT NULL,  
“charisma” integer DEFAULT NULL, “weapon” integer DEFAULT NULL)  
 → 0.1334s  
creatures.rb:28: undefined method `create’ for Creature:Class  
(NoMethodError)  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat creatures.rb  
require ‘active\_record’  
require ‘ArrayOfCreatures’  
require ‘MakeCreature’  
require ‘Creature’

include MakeCreature

ActiveRecord::Base.logger = Logger.new(STDERR)  
ActiveRecord::Base.colorize\_logging = true

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlite3”,  
:dbfile =\> “dwemthys.db”  
)

ActiveRecord::Schema.define do  
create\_table :creatures do |table|  
table.column :life, :integer  
table.column :strength, :integer  
table.column :charisma, :integer  
table.column :weapon, :integer  
end  
end

creature = Creature.create  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat Creature.rb  
require ‘Math’

class Creature

Creature.extend Math  
include Math

attr\_accessor :life, :strength, :charisma, :weapon

def initialize ()  
@life = 10  
@strength = 10  
@charisma = 10  
@weapon = 10  
end

def toString ()  
print “class\t\t”  
print self.class

```
    print "\nlife\t\t"
    print @life

    print "\nstrength\t"
    print @strength

    print "\ncharisma\t"
    print @charisma

    print "\nweapon\t\t"
    print @weapon
    print "\n"

```

end

end  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 22, 2007, 10:45am UTC](https://www.ruby-forum.com/t/undefined-method-create/123936/2 "2007-12-22T10:45:51Z")

</div>

On Sat, 22 Dec 2007 17:12:30 +0900, Thufir wrote:

> I want to create one creature and write it to the dwemthys.db, and I’m  
> close. From the example at \<[http://www.dzone.com/links/rss/](http://www.dzone.com/links/rss/)  
> simple\_ruby\_activerecord\_example.html?\> I know that the “create” method  
> is used for this, but somehow it fails when for creatures when it works  
> for the example.

Got it with some help! 🙂

My code wasn’t following what ActiveRecord required, but this works:

thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ ruby creatures.rb  
– create\_table(:creatures)  
SQL (0.036018) CREATE TABLE creatures (“id” INTEGER PRIMARY KEY NOT  
NULL, “life” integer DEFAULT NULL, “strength” integer DEFAULT NULL,  
“charisma” integer DEFAULT NULL, “weapon” integer DEFAULT NULL)  
 → 0.1568s  
#\<Creature:0xb77f9d44 @new\_record=true, @attributes={“strength”=\>0,  
“charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.000885) INSERT INTO creatures (“strength”, “charisma”,  
“life”,  
“weapon”) VALUES(0, 0, 0, 0)  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ sqlite3 dwemthys.db  
SQLite version 3.4.1  
Enter “.help” for instructions  
sqlite\> select \* from creatures;  
1|0|0|0|0  
sqlite\>  
sqlite\> .quit  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat creatures.rb  
require ‘active\_record’  
require ‘ArrayOfCreatures’  
require ‘MakeCreature’  
require ‘Creature’

include MakeCreature

ActiveRecord::Base.logger = Logger.new(STDERR)  
ActiveRecord::Base.colorize\_logging = true

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlite3”,  
:dbfile =\> “dwemthys.db”  
)

ActiveRecord::Schema.define do  
create\_table :creatures do |table|  
table.column :life, :integer  
table.column :strength, :integer  
table.column :charisma, :integer  
table.column :weapon, :integer  
end  
end

creature = Creature.new  
p creature  
creature.save  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat Creature.rb  
require ‘Math’

class Creature \< ActiveRecord::Base

attr\_accessor :life, :strength, :charisma, :weapon

def to\_s ()  
print “class\t\t”  
print self.class

```
    print "\nlife\t\t"
    print @life

    print "\nstrength\t"
    print @strength

    print "\ncharisma\t"
    print @charisma

    print "\nweapon\t\t"
    print @weapon
    print "\n"

```

end

end  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $

-Thufir
