How to specify limit and offset in array in rails?

sank picture sank · Apr 8, 2015 · Viewed 10.6k times · Source

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.

Answer

Arup Rakshit picture Arup Rakshit · Apr 8, 2015

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]