15

I have 3 tables PostText, PostImage and PostVideo. Now I am combining data from all the above three table into a single array called userposts.

Now from userposts I want to access only 10 records starting with offset 15.

How can I do that?

I tried out userposts.first(10). It gives me first 10 records but I want 10 records starting from offset-15.

thanks in advance.

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
sank
  • 964
  • 3
  • 12
  • 24
  • Eg: If `arr = [1,2,3,4,5,6,7,8,9]`, then this `arr[0..2]` would give `[1,2,3]`, `arr[3..5]` would give `[4,5,6]` – Abhi Nov 21 '18 at 12:38

2 Answers2

25

userposts.drop(15).first(10) will help you

jon snow
  • 3,062
  • 1
  • 19
  • 31
22

You should use ary[start, length] → new_ary or nil method.

..returns a subarray starting at the start index and continuing for length elements,

userposts[10, 15]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 3
    Looks like he's using a query though, if he is then `userposts.offset(15).limit(10)` would be better – j-dexx Apr 08 '15 at 10:15
  • @japed You are right.. But from _..from all the above three table into a single **array** called userposts._ I can infer what I answered.. :) – Arup Rakshit Apr 08 '15 at 10:16
  • @japed it gives me an error that undefined method 'offset' for an Array. – sank Apr 08 '15 at 10:16