Thursday, October 27, 2011

Re: [Rails] Re: Re: Different Order by clause on will_paginate pages?

On Oct 27, 2011, at 10:02 AM, Hussam Ali wrote:

>> I think you're missing one thing here. This precise construction means
>> that there is a significant chance that some items will never be seen at
>> all. If the first "page" is random, it's not "random within the
>> parameters of what would otherwise land on page 1", it's "random across
>> the entire database". And then you move to page 2, where it's created_at
>> ber alles.
>>
>> What I would do is have a separate route for "random tease of what's in
>> the database", and then have a real "everything, in order" route as is
>> normal for an index + pagination.
>
> Thanks Walter, it would certainly miss some of the items, but what you
> are saying to have separate route for random and for pagination is may
> be possible but right now i do not understand how to implement this, but
> i will try to do it and if u can elaborate it further with example then
> it would be helpful for me.

#foos_controller.rb

def sample
@foos = Foo.order('RAND()').limit(20) #mysql
#@foos = Foo.order('RANDOM()').limit(20) #sqlite
end

routes.rb

resources :foos do
collection do
get 'sample'
end
end

#foos/sample.html.erb
<h1>Hey look at all the cool foos we have!</h1>
<%= render @foos %>

#foos/_foo.html.erb
<div class="foo">
<p><%= foo.name %></p>
<p><%= foo.rank %></p>
<p><%= foo.serial_number %></p>
</div>

And then your index.html.erb would be exactly the same as it currently is, with the pagination and whatnot, and your index (controller) method would also be the same, with no worries about making the pagination smarter. Wherever you wanted your random sample to appear, you would simply use the sample method. And your index would be a normal index without any notion of randomness, so any possible foo could be shown or found.

Walter

>
> --
> 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@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment