ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
default_scope ¤Î
±»º¦ˆó¸æ
2015/03/28
minami.rb ×î³õ¤Ç×îáá¤ÎLT´ó»á
ŸoÁ¿¾®?½¡(@muryoimpl)
https://www.?ickr.com/photos/jakerust/16827350035
×Ô¼º½B½é
? ŸoÁ¿¾®?½¡(¤à¤ê¤ç¤¦¤¤?¤±¤ó)
? ÓÀºÍ¥·¥¹¥Æ¥à¥Þ¥Í¥¸¥á¥ó¥È 7¥öÔÂÄ¿
? RubyévÎ÷, évÎ÷Ruby»á×h04?05, ¤ë¤Ó¤Þ etc
? ÐÝÈդϡ¢µÍ™C„ÓÐÍÇÞ¤¿¤­¤ê¶þ•rég¥µ¥¹¥Ú¥ó¥¹ŽüÈË
½Ô¤µ¤ó¤´´æÖª¤À¤È
¤ª¤â¤¤¤Þ¤¹¤¬
https://www.?ickr.com/photos/aruarian/2626408619
https://www.?ickr.com/photos/aruarian/2626408619
default_scope
default_scope
class Item < ActiveRecord::Base
default_scope -> { order(:order_no) }
end
irb(main)> Item.find(1)
SELECT "items".* FROM ¡°items"
WHERE "items"."id" = ?
ORDER BY ¡°items"."order_no"
DESC LIMIT 1 [["id", 1]]
https://www.?ickr.com/photos/lintmachine/3652702115
ºÎ¤â¤·¤Ê¤¯¤Æ¤â
„ÙÊÖ¤Ë
Ìõ¼þ¤¬¤Ä¤­¤Þ¤¹¤Í
https://www.?ickr.com/photos/aruarian/2626408619
±ãÀû¤Ç¤¹¤Í
https://www.?ickr.com/photos/aruarian/2626408619
default_scope
¤ÎÌõ¼þÍ⤹·½·¨
¤¢¤ê¤Þ¤¹¤Í
https://www.?ickr.com/photos/aruarian/2626408619
https://www.?ickr.com/photos/lintmachine/3652702115
unscoped
irb(main)> Item.unscoped.find(1)
SELECT "items".* FROM ¡°items"
WHERE "items"."id" = ?
DESC LIMIT 1 [["id", 1]]
irb(main)> Item.unscoped { Item.find(1) }
SELECT "items".* FROM ¡°items"
WHERE "items"."id" = ?
LIMIT 1 [["id", 1]]
https://www.?ickr.com/photos/lintmachine/3652702115
except
irb(main)> Item.except(:order).find(1)
SELECT "items".* FROM ¡°items"
WHERE "items"."id" = ?
DESC LIMIT 1 [["id", 1]]
https://www.?ickr.com/photos/lintmachine/3652702115
unscope
irb(main)> Item.unscope(:order).find(1)
SELECT "items".* FROM ¡°items"
WHERE "items"."id" = ?
DESC LIMIT 1 [["id", 1]]
Íâ¤ì¤Þ¤·¤¿¤Í£¿
https://www.?ickr.com/photos/aruarian/2626408619
Õ{¤Ù¤ë¤È
¤À¤¤¤¿¤¤¤³¤¦¤¤¤¦Àý¤¬
Ýd¤Ã¤Æ¤¤¤Þ¤¹
https://www.?ickr.com/photos/aruarian/2626408619
¤µ¤Æ
https://www.?ickr.com/photos/aruarian/2626408619
½YºÏ¤È¤«¤·¤Æ¤¿¤é
¤É¤¦¤Ç¤¹¤«¤Í£¿
https://www.?ickr.com/photos/aruarian/2626408619
includes¤·¤Æ¤ß¤ë¤¾
class Author < ActiveRecord::Base
has_many :posts
default_scope -> { where(status: :ok) }
end
class Post < ActiveRecord::Base
belongs_to :author
end
https://www.?ickr.com/photos/lintmachine/3652702115
https://www.?ickr.com/photos/lintmachine/3652702115
includes¤·¤Æ¤ß¤ë¤¾
irb(main)> Post.includes(:author)
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
SELECT "posts"."id" AS t0_r0, ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = ¡°posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
https://www.?ickr.com/photos/lintmachine/3652702115
unscoped
irb(main)> Author.unscoped {
Post.includes(:author)
.where(status: :ok)
.where(authors: {name: ¡®a¡¯}) }
SELECT "posts"."id" AS t0_r0, ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = ¡°posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
Íâ¤ì¤Ê¤¤£¡
https://www.?ickr.com/photos/aruarian/2626408619
https://www.?ickr.com/photos/lintmachine/3652702115
except
irb(main)>
Post.includes(:author).except(:where)
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
SELECT "posts"."id" AS t0_r0, ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = ¡°posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
¤½¤â¤½¤â
ON¾ä¤À¤·¡­
https://www.?ickr.com/photos/aruarian/2626408619
https://www.?ickr.com/photos/lintmachine/3652702115
except
irb(main)> Post.includes(:author)
.except(on: :status) #¤Ê¤¤¤è¤Í¡­ŸoÒ•¤µ¤ì¤ë
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
SELECT "posts"."id" AS t0_r0, ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = ¡°posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
https://www.?ickr.com/photos/lintmachine/3652702115
unscope
irb(main)> Post.includes(:author)
.unscope(on: :status) #¤Ê¤¤¤è¤Í¡­
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
ArgumentError: Hash arguments
in .unscope(*args) must have :where as the
key.
https://www.?ickr.com/photos/lintmachine/3652702115
unscope
irb(main)> Post.includes(:author)
.unscope(where: :status)
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
SELECT "posts"."id" AS t0_r0 ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = "posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
„e¤ÎévßB¤Ä¤¯¤Ã¤Æ¡­
class Author < ActiveRecord::Base
has_many :posts
default_scope -> { where(status: :ok) }
end
class Post < ActiveRecord::Base
belongs_to :author
belongs_to :unscoped_author, ->
{ unscope(where: :status) },
foreign_key: :author_id,
class_name: ¡®Author¡¯
end https://www.?ickr.com/photos/lintmachine/3652702115
https://www.?ickr.com/photos/lintmachine/3652702115
„e¤ÎévßB¤Ä¤¯¤Ã¤Æ¡­
irb(main)> Post.includes(:unscoped_author)
.where(status: :ok)
.where(authors: {name: ¡®a¡¯})
SELECT "posts"."id" AS t0_r0, ¡­
FROM "posts"
LEFT OUTER JOIN "authors"
ON "authors"."id" = ¡°posts"."author_id"
AND "authors"."status" = ?
WHERE "authors"."name" = ?
[["status", 1], ["name", "a"]]
¤ä¤Ï¤ê
Íâ¤ì¤Ê¤¤!
https://www.?ickr.com/photos/aruarian/2626408619
https://www.?ickr.com/photos/alvarez-tostado/363243449
¥Þ ¥Þ ©`©`©`
https://www.?ickr.com/photos/alvarez-tostado/363243449
¤È¤¤¤¦¤³¤È¤Ç¡­
https://www.?ickr.com/photos/alvarez-tostado/363243449
Æü¤­¤Ê¤¬¤é
¤³¤¦¤·¤¿¡­
default_scopes =[]
class Author < ActiveRecord::Base
has_many :posts
default_scope -> { where(status: :ok) }
end
class UnscopedAuthor < Author
# ¡ý¤³¤³¤¬¥Ý¥¤¥ó¥È
self.default_scopes = []
end
class Post < ActiveRecord::Base
belongs_to :author
belongs_to :unscoped_author,
foreign_key :author_id
end https://www.?ickr.com/photos/lintmachine/3652702115
default_scope¤µ¤ó¡­
? ¤³¤ó¤ÊÁF¤¬¤¢¤ê¤Þ¤·¤¿¡­
? …gÒ»¥â¥Ç¥ë¤·¤«—ÊË÷¤·¤Ê¤¤ég¤Ï±ãÀû¤µ¤·¤«¸Ð¤¸¤Ê¤¤
¤«¤â¤·¤ì¤Þ¤»¤ó¤¬¡­SQLµÄ¤ËJOIN¤¹¤ë¤³¤È¤Ë¤Ê¤ë
¤ÈÃæµ¹¤Ê¤³¤È¤Ë¤Ê¤ê¤Þ¤¹(¤Ê¤ê¤Þ¤·¤¿)
? ÉÙ¤·Ãæµ¹¤Ê¥¯¥¨¥ê¤ò¿¼¤¨¤ë¤È¤­¤ËÖ±Ã椹¤ë¤Î¤Ç¤¹¤è
¡­¤³¤Î†–î}
? áá»Ú¤·¤Ê¤¤¤è¤¦¤Ë¡¢scope¤òš°»Ø¤Ä¤±¤ë¤«ÔOÓ‹¤ò
ÒŠÖ±¤¹¤Û¤¦¤¬Á¼¤¤¤È˼¤¤¤Þ¤¹
https://www.?ickr.com/photos/71508688@N00/5139944772

More Related Content

Default scope¤Î±»º¦ˆó¸æ