Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify limit and offset in array in rails?

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.

like image 836
sank Avatar asked Apr 08 '15 10:04

sank


2 Answers

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

like image 111
jon snow Avatar answered Nov 04 '22 21:11

jon snow


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]
like image 22
Arup Rakshit Avatar answered Nov 04 '22 20:11

Arup Rakshit