ruby - Retrieving only the sixth and seventh matching results in a Rails table -
i working on rails app, , hitting roadblock: need return several items in table are, say, 6th-8th matching items. ironically, rails has helper method shown here .first
through .fifth.
my question is: how can find these results when n searching 6,7,8 , on without calling of 1st through 8th items?
here's why doing this. making home page blog. @ top, want show 5 recent posts (which shown image). underneath them, want show additional 3 links posts text.
currently, doing first part fine. in blog.controller.rb
:
@posts = post.order('created_at desc').first(5)
i iterate through posts in view, , shows first 5 recent results. want show 6th-8th, , try , store them in new method. change controller to:
@posts = post.order('created_at desc').first(5) @texts = post.order('created_at desc').limit(8)
this returns 8 recent posts instead of, say, 8th.
is possible me limit @texts
show matching 6th, 7th, , 8th results?
yes. you'll want use offset them. here's documentation on offset: http://apidock.com/rails/activerecord/querymethods/offset
post.order(created_at: :desc).limit(3).offset(5)
Comments
Post a Comment