繼續前進,並採取ActiveRecord暴跌,這裏是一些入門的h2嵌入式java數據庫。
我想你需要這些寶石
jruby -S gem install active_record
jruby -S gem install active_record_jdbc_adapter
jruby -S gem install active_record_jdbch2_adapter
jruby -S gem install jdbc_h2
然後你可以使用活動記錄這樣
require 'active_record'
require 'logger'
my_logger = Logger.new(STDOUT)
my_logger.level = Logger::DEBUG
ActiveRecord::Base.logger = my_logger
ActiveRecord::Base.establish_connection(
:adapter => 'jdbch2',
:database => "my_database_file", # set to anything you want first run
:username => "my_username", # set to anything you want first run
:password => "my_secret_password" # set to anything you want first run
)
你需要有一些表格插入到。保持ActiveRecord約定,維護一個排序的數據庫模式更改文件夾,然後將該遷移器指向該文件夾。例如,創建一個「遷移」文件夾,以及名爲「20090815230000_create_my_models.rb」的以下文件。確保「snake_case」文件名與CamelCase類名相匹配。
class CreateMyModels < ActiveRecord::Migration
def self.up
create_table :my_models do |t|
t.string :foo
end
end
def self.down
drop_table :my_models
end
end
現在(返回到您的主腳本),您可以將Rails遷移器指向此文件夾。 rails migrator存儲所有元數據,以便按順序運行遷移,並在新遷移可用時運行新遷移。
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("migrations")
一旦你創建你的表,你可以使用ActiveRecord正常
class MyModel < ActiveRecord::Base
end
現在你可以插入東西到數據庫中。
x=MyModel.new
x.foo="bar"
x.save!
我希望這有助於。如果ActiveRecord太辛苦了,Sequel很輕鬆有趣。
我真的不會使用該文件。 去爲[Hpricot](http://wiki.github.com/hpricot/hpricot)或[Nokogiri](http://nokogiri.org) –