Rails ã§ååã«æ´»ç¨ããã¦ããªãã¦ãã£ãããªã ActiveRecord::Relation ã®ã¡ã½ãã TOP 10
2013å¹´12æ2æ¥æ´æ°: åç §ããããã¨ãå¤ãã®ã§ Rails 4 ã®æ å ±ã訳注ã¨ãã¦è¿½è¨ãã¾ãããã¾ããRails 4 ã«é¢ããæ å ±ã¯ã WEB+DB PRESS Vol.73 ãé常ã«åèã«ãªãã®ã§ãä¸èªããããããã¾ãã
ãã®æç« ã¯ Mitch Crowe æ°ã®ããã°ãã 2012å¹´4æ14æ¥ã®è¨äºã翻訳ãããã®ã§ãã
The 10 Most Underused ActiveRecord::Relation Methods
http://blog.mitchcrowe.com/blog/2012/04/14/10-most-underused-activerecord-relation-methods/
æ¨æ¥ã¯ ActiveRecord::Relation ã®ã³ã¼ãã«èã¾ã§æµ¸ãã£ã¦ã使ããã¦ããã®ãããã¾ã§å
¨ç¶è¦ããã¨ããªãé¢ç½ããã²ãããæãåºããã¦ãããããã®è¨äºã§ãååã«æ´»ç¨ããã¦ããªã Relation ã¯ã©ã¹ã®ã¡ã½ããã®ããã10ããªã¹ãã«ããã®ã§ã楽ããã§ãããããã
10ä½ ãããã¯ä»ãã® first_or_create
first_or_create ã¯ã¨ã¦ããªãã¿æ·±ãã
Book.where(:title => 'Tale of Two Cities').first_or_create
ããã¦ãååéãã®ãã¨ããã£ã¦ããããã ããç¹å®ã®å±æ§ãæã¤ã¬ã³ã¼ãã find ããã¨ãããããã®å±æ§ãæã¤ã¬ã³ã¼ãã create ãã¦ãããã«è¿½å ã§å±æ§ãè¨å®ããããªããã¨ãé »ç¹ã«ããã¨æãããããç°¡æ½ã«ããã«ã¯ first_or_create ã«ãããã¯ãä¸ããã°ããã
Book.where(:title => 'Tale of Two Cities').first_or_create do |book| book.author = 'Charles Dickens' book.published_year = 1859 end
9ä½ first_or_initialize
ãã®ã¬ã³ã¼ããã¾ã ä¿åããããªãå ´åã¯ã first_or_initialize ã使ããã
Book.where(:title => 'Tale of Two Cities').first_or_initialize
8ä½ scoped
ããã¯ã©ã¹ã®æã¤ãã¹ã¦ã®ã¬ã³ã¼ãã表ãã ActiveRecord::Relation ãã»ãããã¨ãããããããªã¨ã㯠scoped ã¡ã½ããã使ãã°ç°¡åã«çæã§ããã(訳注: Rails 4 ã§ã¯ã scoped ãéæ¨å¥¨ã«ãªãã all ã Array ãã ActiveRecord::Relation ã®ãªãã¸ã§ã¯ããè¿ãããã«ãªãã¾ãããRails 4 ã§ã¯ scoped ãã all ã«å¤æ´ãã¦ãã ããã)
def search(query) if query.blank? scoped else q = "%#{query}%" where("title like ? or author like ?", q, q) end end
7ä½ none ( Rails 4 ã®ã¿ï¼
åãããã«ããªãã¸ã§ã¯ããå«ã¾ãªã ActiveRecord::Relation ãã»ãããã¨ãããã空㮠Array ãè¿ããã¨ãã大æµããã»ã©ãããªãã®ã¯ã API ã®å©ç¨è ã Relation ãªãã¸ã§ã¯ããæå¾ ãã¦ããããã ã代ããã« none ã使ãã°ããã
def filter(filter_name) case filter_name when :all scoped when :published where(:published => true) when :unpublished where(:published => false) else none end end
注æï¼æå 端ãè¡ã人ã¯ä»ãã none ã使ããããªã£ã¦ãããã¨ã ã¨æããããã¯ã Rails 3 ã§ã¯ãªãã Rails 4 ã§å©ç¨ã§ãããã ããRails 4 ãå¾ ã¤ã¾ã§ã®éãç°¡åã«æ¸ããã¨ãã§ããã この Stack Overflow のスレッドããã§ãã¯ãã¦ã»ããã
6ä½ find_each
æ°åã¬ã³ã¼ããã¤ã¬ãã¼ããããããªã£å ´åãeach ã使ãããã¯ãªãã ããã
each ã¯1 ã¤ã®ã¯ã¨ãªãå®è¡ãã¦ãå
¨ã¬ã³ã¼ããåå¾ããããããã¹ã¦ãã¡ã¢ãªä¸ã«ã¤ã³ã¹ã¿ã³ã¹åãã¦ãã¾ããã¡ã¢ãªãååã«ç¢ºä¿ãã¦ãããªãããã確ä¿ãã¦ãªããã°ããã㯠Rails ã¢ããªãããªã¼ãºãããç´ æµãªæ¹æ³ã«ãªããfind_each ã¯ãããã§ã¯ãªããã¬ã³ã¼ãããããå¦ç㧠find ãã¦ï¼ããã©ã«ãã¯1000件ï¼ãä¸åº¦ã« yield ããããã®çµæãåæã«å
¨ã¬ã³ã¼ãåã®ã¡ã¢ãªã確ä¿ããå¿
è¦ããªããªãã
find_each 㯠yield ãããã¬ã³ã¼ãã®é åºãæå®ã§ããªãã®ã§æ³¨æã ãæå®ãã¦ããã åã«ç¡è¦ãããã
Book.where(:published => true).find_each do |book| puts "Do something with #{book.title} here!" end
5ä½ to_sql 㨠explain
ActiveRecord ã¯ç´ æ´ãããããæã£ãéãã®ã¯ã¨ãªããã¤ãçæãã¦ãããã¨ã¯éããªããã³ã³ã½ã¼ã«ã«é£ãã§ãçµã¿ç«ã¦ã Relation 㧠ãã® 2 ã¤ã®å½ä»¤ãå®è¡ãã¦ã¿ãããè³¢ãã¯ã¨ãªã«ãããããã¦ããã確èªãã¦ããããªã£ã¦ãªãã£ããææ è¾¼ãã¦ä½ã£ãã¤ã³ããã¯ã¹ã使ãããã«ãããã
Library.joins(:book).to_sql # => SQL query for you database. Libray.joins(:book).explain # => Database explain for the query.
4ä½ find_byï¼Rails 4 ã®ã¿ï¼
Rails ã§æ¸ããã³ã¼ãã¯æ¬¡ã®ãããªè¡ã§æ£ããã£ããã®ã«ãªããããã
Book.where(:title => 'Three Day Road', :author => 'Joseph Boyden').first
代ããã«ã find_by ã¨ããã·ã§ã¼ãã«ããã¡ã½ããã使ããã
Book.find_by(:title => 'Three Day Road', :author => 'Joseph Boyden')
ããã¯ä¸ã¨åããã®ãå®è¡ãããã
注æï¼æå 端ãè¡ã人ã¯ä»ãã find_by ã使ããããªã£ã¦ãããã¨ã ã¨æãããã㯠Rails 3 ã§ã¯ãªãã Rails 4 ã§å©ç¨ã§ããã
3ä½ scoping
scope ã¡ã½ãããç¹å®ã® Relation ã¨ãã¦ä½¿ãããRails ã®ããã¥ã¡ã³ãã«ãã次ã®ä¾ãæ¤è¨ãã¦ã¿ããã
Comment.where(:post_id => 1).scoping do Comment.first # SELECT * FROM comments WHERE post_id = 1 end
ããã¯ã¾ã£ãããã£ã¦ä½¿ããããã
2ä½ pluck
ç¹å®ã®ã¬ã³ã¼ãã®ã«ã©ã ã§é åã«ããããã¨ããªãã ãããï¼ç§ã¯ããããã«ãæ¬å½ã«ããããè¦ã¦ããã
published_book_titles = Book.published.select(:title).map(&:title)
ããã«æªãå ´åã ã¨ããã ã
published_book_titles = Book.published.map(&:title)
代ããã« pluck ã使ããã
published_book_titles = Book.published.pluck(:title)
(訳注: Rails 3 ã§ã¯ãpluck ã®å¼æ°ã«æå®ã§ããã·ã³ãã«ã¯ 1 ã¤ã§ãããã Rails 4 ã§ã¯ã·ã³ãã«ã®é åãæå®ãããã¨ã§ãå±æ§ãè¤æ°æå®ãããã¨ãã§ãã¾ããselect ã§è¤æ°ã®å±æ§ãæå®ããå ´åã¨éãã2次å é åãè¿ãã¾ãã3 ç³»ã§ãæ¯è¼çæè¿ã®ãã¼ã¸ã§ã³ã§ä½¿ããã¯ãã§ãã)
1ä½ merge
åã¯ãã®å®ç³ãªãã«ã¯çããããªãããããå¥å¦ãªãã¨ã«ãããã¯ã½ã¼ã¹ä¸ã«ããã¥ã¡ã³ãããã¦ããªãããä»ã¾ã§è¦ãã¬ã¤ãã§ãè¨åããªããããã使ãã¨ãçµå( JOIN )ãã§ãã¦ãçµåãããã¢ãã«ã«å¯¾ãã¦ååä»ãã¹ã³ã¼ãã§ãã£ã«ã¿ã§ããã(訳注: ç§è¦ã§ããã4.0.0 ã§ã¯ merge ãæå³ã¨éãåä½ãããªãããã確èªããæ¹ãããã¨æãã¾ãã*1 ããã¤ãä¿®æ£ãå ¥ã£ã¦ãã4.0.1 以éã§ã試ãã¦ã¿ã¦ãã ããã)
class Account < ActiveRecord::Base # ... # Returns all the accounts that have unread messages. def self.with_unread_messages joins(:messages).merge( Message.unread ) end end
- ä½è : è¨æ¨æ´ç¾,ç½åæ §,ã¯ã¾ã¡ã2,大åç°ç´,æ¾ç°æ,å¾è¤å¤§è¼,ã²ããã¾ããã,å°æ篤,è¿è¤å®æºæ,ã¾ãã¾ãè¬è¥æ³¢ç¾ è,Mr. O,å·æ·»è²´ç,éå½åå®,æ³æ¾¤å»ºå¤ªé,奥éå¹¹ä¹,ä½è¤éå¹³,å¾è¤ç§å®£,mala,ä¸å³¶è¡,å ¤æºä»£,森ç°åµ,A-Listers,WEB+DB PRESSç·¨éé¨
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2013/02/23
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 12人 ã¯ãªãã¯: 131å
- ãã®ååãå«ãããã° (7件) ãè¦ã