Undefined method `create'

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/
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, but this is
what I’m running into:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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)
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $

thanks,

Thufir

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/
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! :slight_smile:

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

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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)
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $

-Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs