Skip to content

Instantly share code, notes, and snippets.

@hnaohiro
Created February 11, 2013 17:58
Show Gist options
  • Save hnaohiro/4756183 to your computer and use it in GitHub Desktop.
Save hnaohiro/4756183 to your computer and use it in GitHub Desktop.
Ruby単体でのActive Recordのサンプル
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'db/hatena.db'
)
class EntryInit < ActiveRecord::Migration
def self.up
create_table(:entries) do |t|
t.column :link, :text
t.column :title, :text
t.column :count, :integer
t.column :content, :text
end
end
def self.down
drop_table :entries
end
end
if not ActiveRecord::Base.connection.table_exists? :entries
EntryInit.migrate(:up)
end
class Entry < ActiveRecord::Base
def to_s
return "#{title}: #{count}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment