Skip to content

Instantly share code, notes, and snippets.

@Naggi-Goishi
Last active May 10, 2017 13:51
Show Gist options
  • Save Naggi-Goishi/e216b3262827a57ae653a99712b22399 to your computer and use it in GitHub Desktop.
Save Naggi-Goishi/e216b3262827a57ae653a99712b22399 to your computer and use it in GitHub Desktop.
ActiveRecord::QueryMethods#limitとActiveRecord::FinderMethods#take ref: http://qiita.com/Naggi-Goishi/items/856a7698f167efdb2a17
twelves = User.where(age: 12)
# ActiveRecord::QueryMethods#limitを使用する
twelves.limit(3)
# User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12' LIMIT 3
#=> <#ActiveRecord::Relation [<#User id: 1, name: "ゴン=フリークス", age: 12>, <#User id: 2, name: "キルア=ゾルディック", age: 12>, <#User id: 3, name: "ピーター・パン", age: 12>]>
# ActiveRecord::FinderMethods#takeを使用する
twelves.take(3)
#=> [<#User id: 1, name: "ゴン=フリークス", age: 12>, <#User id: 2, name: "キルア=ゾルディック", age: 12>, <#User id: 3, name: "ピーター・パン", age: 12>]
twelves.limit(3).class
# => User::ActiveRecord_Relation
twelves.take(3).class
# => Array
User.where(age: 12).take(3)
User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12'
User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12' LIMIT 3
twelves = User.where(age: 12).limit(3)
twelves = twelves.limit(5)
twelves = User.where(age: 12).limit(3)
User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12' LIMIT 3
twelves = twelves.limit(5)
User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12' LIMIT 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment