LoginSignup
5
5

More than 5 years have passed since last update.

ActiveRecord 単体にテーブル・カラムを用意してもらうメモ

Last updated at Posted at 2014-04-15

とくに解説とかはしない。めも。

db.yml
development:
  adapter: 'sqlite3'
  database: './list.db'
db.rb
require 'active_record'
require 'yaml'

class List < ActiveRecord::Base
end

config = YAML.load_file('./config/db.yml')
p config
ActiveRecord::Base.establish_connection config['development']

ActiveRecord::Schema.define do
  unless ActiveRecord::Base.connection.tables.include? 'lists'
    create_table :lists do |table|
      table.column :user_name,    :string
      table.column :date_string,  :string
    end
  end
end
main.rb
require_relative './db'

List.create(user_name: 'name', date_string: '2014-01-01')
List.find_by_user_name('name')

Link

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5