FixturesをRails外で使ってみる
思いっきりActiveRecord依存なんですね。
まずはpeople.yml
first: id: 1 name: test second: id: 2 name: hoge <% 10.times do |i| %> loop<%= i %>: id: <%= (i+3) %> name: <%= "hoge#{i}" %> <% end %>
次にフィクスチャを使うサンプルコード。
ActiveRecord-JDBC使ってます。
require 'rubygems' require 'active_record' require 'active_record/fixtures' ActiveRecord::Base.establish_connection(:adapter => "jdbcderby", :database => "hoge") # 初回実行時は、このコメントをはずす #con = ActiveRecord::Base.connection #con.execute("CREATE TABLE PEOPLE(id int, name char(30))") puts "insert" fixtures = Fixtures.create_fixtures("data", ["PEOPLE"]) class Person < ActiveRecord::Base end puts "select all" Person.find(:all, :order => "ID").each do |p| puts p.id.to_s + "/" + p.name end puts "delete" fixtures.delete_existing_fixtures puts "select all" Person.find(:all).each do |p| puts p.to_s end puts "finish"
結果はこう。
insert select all 1/test 2/hoge 3/hoge0 4/hoge1 5/hoge2 6/hoge3 7/hoge4 8/hoge5 9/hoge6 10/hoge7 11/hoge8 12/hoge9 delete select all finish
動的フィクスチャも普通に動いてますね。