elikem
7/7/2014 - 3:20 AM

active record and migrations single file

active record and migrations single file

# Create a new database each time
File.delete 'dbfile' if File.exist? 'dbfile'

require 'active_record'
ActiveRecord::Base.establish_connection :adapter => "sqlite3",
                                        :database => "dbfile"

# Initialize the database schema
ActiveRecord::Base.connection.create_table :ducks do |t|
   t.string  :name
end

class Duck < ActiveRecord::Base
  validates_length_of :name, :maximum => 6
end

my_duck = Duck.new
my_duck.name = "Donald"
my_duck.valid?         # => true
my_duck.save!

some_duck = Duck.find(1)
some_duck.id           # => 1
some_duck.name         # => "Donald"
some_duck.delete