Created
          February 11, 2013 17:58 
        
      - 
      
 - 
        
Save hnaohiro/4756183 to your computer and use it in GitHub Desktop.  
    Ruby単体でのActive Recordのサンプル
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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