On 25 January 2016 at 13:19, Matti Ezzat <lists@ruby-forum.com> wrote:
> Hi yes I did the below codes
>
> def redirect_to_random
> @restaurant = Restaurant.where(vegan: false).take!
> render 'show'
> end
>
> the only problem is only showing one result with a vegan false, what I
> want is when ever I push suggestion button every time I get a different
> result but with vegan false..
I don't see what that has got to do with your original question. Are
you in fact asking how to return a random record from an activerecord
query?
The take method does not give you a random record, it is just that
which one it gives is not determined. I have never had to do that so
googled for
activerecord random record and the first result [1] suggests the following
offset = rand(Restaurant.where(vegan: false).count)
# Rails 4
@restaurant = Restaurant.where(vegan: false).offset(offset).first
# Rails 3
@restaurant = Restaurant.where(vegan: false).first(:offset => offset)
I suggest some further googling to see whether there are other suggestions.
Colin
[1] http://stackoverflow.com/questions/2752231/random-record-in-activerecord
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLu7orKMgEtoOCTbcyOypgYB5KhEK2LXBcnTaL6y%2By%2BPfg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment