Reputation: 693
I need a way to get the first n items of a model. Item.first(n)
, Item.all[1..n]
will do that, except they return an array, not an object.
How do I get it as an ActiveRecord Object?
irb(main):135:0> Player.where(game_id: 1).class
=> Player::ActiveRecord_Relation #Ok
irb(main):136:0> Game.first.players.class
=> Player::ActiveRecord_Associations_CollectionProxy #Ok
irb(main):137:0> Player.where(game_id: 1).first(2).class
=> Array #Not Ok
I want to run update_all
on the returned collection of players, and I can't do that on an array.
Upvotes: 6
Views: 3721
Reputation: 26788
You do .limit(n)
Combine this with .offset
and you have pagination.
Upvotes: 9