Heroku経由でSinatraを使ってみているのですが、ModelとしてSequelを使ってみることにしました。

###Userクラスを作る
DBをオンメモリにしておいて、テーブル作成・クラスの宣言
shell>>
% irb
require 'rubygems'
require 'sequel'

DB = Sequel.sqlite
DB << "CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR(255) not NULL)"

class User < Sequel::Model
end
<<--

これから、User.なんとかでデータの作成などできます。

shell>>
User.create(:name => 'satoko')

User.find(:name => 'satoko')
User.find(:id => 1).update(:name => 'satoko')
User.find(:id => 1).delete
User.first

User.all
=> [#<User @values={:name=>"satoko", :id=>1}>]

DB[:users].filter(:name => 'satoko').sql
=> "SELECT * FROM users WHERE (name = 'satoko')"
<<--

###findは1件操作なので、複数件の場合はfilterで行う
shell>>
User.filter("id > ?", 0).limit(10).each {|u| p[:name]}
=> #<Sequel::SQLite::Dataset: "SELECT * FROM users WHERE (id > 0)">
<<--

また、@orepuriさんに教えてもらったのですが、all、first、eachなどがあって初めてSQLが発行されるそうで、なかなか面白いです

###References
Sequel概要(2年前の資料なので、コード的には古いかもしれません)
http://www.slideshare.net/zhesto/ruby-off-rails-japanese

チートシート
http://sequel.rubyforge.org/rdoc/files/doc/cheat_sheet_rdoc.html
http://cheat.errtheblog.com/s/sequel/

Classリファレンス:ModelやDatasetなど
http://sequel.rubyforge.org/documentation.html

posted by satoko satoko on Wed 16 Sep 2009 at 21:30 with 0 comments
Contents rssrss
NSAssertやNSLogを無効にする:-DNS_BLOCK_ASSERTIONS=1とNS_BLOCK_ASSERTIONS
GDBでview.frameを表示させる
Core Animationについてのリンク集
[iPhone] new BSDライセンスについてライセンス引用のメモ
Implementing a Container View Controller in UIViewController Class Reference
UITableView用のNSIndexPathの作り方
Xcodeのテンプレート、View-based ApplicationとWindow-based Applicationの違い
CopyOnSelectのLion対応
Xcode4のCode Snippetsを別のPCに移動する+gitで管理する
[iPhone] Singleton: iOS4以降はGCDのdispatch_onceを使う
Tags
iphone開発gitIBInterfaceBuilderiOSSIMBLSnippetsterminaltwitterxcodeXcode4
Comments rssrss
https://buy-property-uae.com/ https://buy-property-uae.com/: Many international investors believe, in today's e... 04/24 00:16
https://www.gms4you.us/ https://www.gms4you.us/: Reliable reviews confirm, in today's world using l... 04/21 19:41
https://rootskitchen.bar/ https://rootskitchen.bar/: Finished a deadly evening session punting on crash... 04/18 12:17
https://rootskitchen.bar/ https://rootskitchen.bar/: Just wrapped up a cracking arvo session grinding c... 04/17 02:19
https://si-design.ru/o-nas https://si-design.ru/o-nas: Цена дизайн-проекта стабильна и не изменится к кон... 04/10 17:15
https://corporativ-luxe.ru/magiya-solnechnoy-kapli https://corporativ-luxe.ru/magiya-solnechnoy-kapli: Мы работаем для вас без выходных и праздников htt... 04/10 16:20
https://si-design.ru/reutov https://si-design.ru/reutov: Интерьер, оформленный в стиле модернизм, отличаетс... 04/08 06:24
https://sbornye-gruzy-kitay.ru/ https://sbornye-gruzy-kitay.ru/: Реально ли привезти косметику? Там же нужны особые... 04/08 03:06