How to specify the limit and offset in an array in rails?

I have 3 tables PostText , PostImage and PostVideo . Now I will combine the data from all of the above three tables into one array named userposts .

Now from userposts I want to access only 10 entries, starting at offset 15.

How can i do this?

I tried userposts.first(10) . This gives me the first 10 records, but I want 10 records to start at offset-15.

early.

+5
source share
2 answers

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

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

 userposts[10, 15] 
+10
source

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

+10
source

All Articles