Hey all, When I do: @posts = Post.find(:all)[0,5] it gives me the first 5 posts But when I do: @posts = Post.find(:all)[10,20] it gives me something like 20 posts or more, shouldn''t this give me only 10 post from 10 to 20? what am I doing wrong? thanx in advance Pat --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 15, 7:32 pm, "Patrick Aljord" <patc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey all, > When I do: > @posts = Post.find(:all)[0,5] > it gives me the first 5 posts > > But when I do: > @posts = Post.find(:all)[10,20] > it gives me something like 20 posts or more, shouldn''t this give me only 10 > post from 10 to 20? > what am I doing wrong? > > thanx in advance > > PatNo, on an array it works like this.. array[offset, length] So Post.find(:all)[10,20] will give you 20 posts starting at number 10. _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> But when I do: > @posts = Post.find(:all)[10,20] > it gives me something like 20 posts or more, shouldn''t this give me only > 10 > post from 10 to 20? > what am I doing wrong?You want to use the range syntax, like arr[10..20] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 15, 2006, at 5:30 PM, _Kevin wrote:> On Oct 15, 7:32 pm, "Patrick Aljord" <patc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hey all, >> When I do: >> @posts = Post.find(:all)[0,5] >> it gives me the first 5 posts >> >> But when I do: >> @posts = Post.find(:all)[10,20] >> it gives me something like 20 posts or more, shouldn''t this give >> me only 10 >> post from 10 to 20? >> what am I doing wrong? > > No, on an array it works like this.. > > array[offset, length] > > So Post.find(:all)[10,20] will give you 20 posts starting at number > 10.$ ri Array#[] --------------------------------------------------------------- Array#[] array[index] -> obj or nil array[start, length] -> an_array or nil array[range] -> an_array or nil array.slice(index) -> obj or nil array.slice(start, length) -> an_array or nil array.slice(range) -> an_array or nil ------------------------------------------------------------------------ Element Reference---Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range. a = [ "a", "b", "c", "d", "e" ] a[2] + a[0] + a[1] #=> "cab" a[6] #=> nil a[1, 2] #=> [ "b", "c" ] a[1..3] #=> [ "b", "c", "d" ] a[4..7] #=> [ "e" ] a[6..10] #=> nil a[-3, 3] #=> [ "c", "d", "e" ] # special cases a[5] #=> nil a[5, 1] #=> [] a[5..10] #=> [] -- Eric Hodel - drbrain-48TerJ1FxhPk1uMJSBkQmQ@public.gmane.org - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---