From: Thufir Date: 2007-12-22T18:45:16+09:00 Subject: Re: undefined method `create' 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 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 #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