Last active
August 29, 2015 14:21
-
-
Save chuck0523/eedd73805e6a018923e2 to your computer and use it in GitHub Desktop.
This file contains 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を使用 | |
require 'logger' #Logを使用 | |
#テーブルに対する定義 | |
ActiveRecord::Base.establish_connection( | |
"adapter" => "sqlite3", | |
"database" => "./blog.db" | |
) | |
#これでActiveRecord内のログを吐き出してくれます。 | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
#クラスの定義 | |
class Post < ActiveRecord::Base | |
#Commetを複数持つことを定義 | |
has_many :comments, :dependent => :destroy | |
#追加レコードの条件を定義 | |
validates :title, :presence => true #titleは必須 | |
validates :body, :length => {:minimum => 5} #本文は5文字以上 | |
#レコード抽出方法をメソッド定義 | |
scope :top3, order("created_at").limit(3) | |
end | |
#クラスはテーブルごとに定義するっぽい | |
class Comment < ActiveRecord::Base | |
#Postに属することを定義 | |
belongs_to :post | |
end | |
#レコード追加1 | |
post = Post.new(:title => "title1", :body => "hello!") | |
post.save | |
#レコード追加2 | |
post = Post.new | |
post.title = "title2" | |
post.body = "hello2" | |
post.save | |
#レコード追加3 | |
post = Post.new do |p| | |
p.title = "title3" | |
p.body = "hello3" | |
end | |
post.save | |
#レコード追加4(この方法だとsaveする必要がない) | |
Post.create(:title => "title4", :body => "hello4") | |
#レコード抽出 | |
p Post.all #全件表示 | |
p Post.first #最初の一件を表示 | |
p Post.last.title #最後の一件のTitleを表示 | |
p Post.find(3) #idが3の要素を表示 | |
#レコード抽出(複数条件) | |
p Post.find_by_title_and_id("title2", 3) | |
# whereを使ったレコード抽出 | |
p Post.where(:title => "title1", :id => 1) | |
p Post.where("title = ? and id = ?","title1",1) | |
p Post.where("title = :title and id = :id", {:title => "title1", :id => 1}) | |
#whereの中に比較演算子を用いたレコード抽出 | |
p Post.where("id > ?",2) | |
p Post.where("body like ?", "hello%") | |
p Post.where(:id => 1..3) | |
p Post.where(:id => [1,3]) | |
#レコード抽出し、降順で表示 | |
p Post.order("id desc").limit(3) | |
#クラスで定義した方法でレコード抽出 | |
p Post.top3 | |
#レコード抽出を試み、存在していなかったら新規作成 | |
Post.where(:title => "title5").first_or_create | |
Post.where(:title => "title6").first_or_create do |p| | |
p.body = "hello6" | |
end | |
#レコード内容更新 | |
post = Post.find(1) | |
post.title = "(new)title1" | |
post.save | |
#レコード内容更新 | |
post.update_attribute(:title, "(new2)title1") | |
post.update_attributes(:title => "nnn", :body => "hhh") | |
#条件に合致したレコードの内容を更新 | |
Post.where(:id => 1..2).update_all(:title => "nnn2", :body => "hhh2") | |
#レコード削除 | |
p Post.where(:id => 1..2).delete_all | |
Post.find(3).destroy | |
#レコード作成(ただし本文の文字の長さが足りないのでエラー) | |
post = Post.new(:body => "123") | |
#保存(例外を投げる) | |
post.save! | |
#保存できなかったら例外を投げる | |
if !post.save | |
p.post.errors.message | |
end | |
#Post側のレコードをまず抽出して、そのレコードが保有するCommentを全件表示 | |
post = Post.find(1) | |
post.comments.each do |comment| | |
p comment.body | |
end | |
#レコードが削除されていることの確認 | |
p Post.all | |
p Comment.all | |
Post.find(1).destroy | |
p Post.all | |
p Comment.all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment