check_box if true or false

Hi everyone...

I have an application in rails, and I need help in something...

my application has two buttons one is to suggest a restaurant with vegan
and another without vegan food, the search is working but what I want is
that the buttons of non vegan should only search is the check_box is 0
thats mean false, the same thing for the suggest with vegan button
should only show the results of the check_box 1 or true..below are my
codes

in Controller
  def redirect_to_random
     @restaurant = Restaurant.offset(rand(Restaurant.count)).first
     render 'show'
  end

  def redirect_to_random_vegan
     @restaurant = Restaurant.offset(rand(Restaurant.count)).first
     render 'show'
  end

in the view
  <div class="field">
    <%= f.label :vegan %>:
    <%= f.check_box :vegan %>
  </div>
as Boolean

Please be more specific about what help you need. Is it that a
particular bit of your code is not working, or is it that you don't
know how to implement a particular feature? For example your question
talks about a checkbox. Is it that you don't know how to access the
checkbox value from the code or is it that you are getting an error
when you run the code, or is it that you can access the value ok but
don't know how to do the search? Or what?

Colin

Hi Colin

Thank you for your replay, what I just want is if I click on the
suggestion button I get a specific result that depend on the check_box
if its checked or not.......the codes work but it show all the results
not a specific ones

Still too general I am afraid. The first part of the problem is to
pass the checkbox value with the request so that it can be tested in
the code. Are you able to do that?
The second part of the problem is to run only the appropriate search.
Don't worry about that till you have solved the first part. Writing
code is always a matter of splitting the requirement down into small
parts and solving them one at a time.

Colin

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

the below codes are not working, What I want is just random result with
specific option

What below codes, there is nothing below in your email. Remember this
is a mailing list not a forum (though you may be accessing it via a
forum-like interface), so if you don't quote the previous message then
we have to go searching back through previous posts.

I assume that you mean this code:
offset = rand(Restaurant.where(vegan: false).count)
@restaurant = Restaurant.where(vegan: false).offset(offset).first

If so then what do you mean by 'it is not working'? Do you mean that
it is not returning a random record where vegan is false, or do you
mean that you do not want a random record with vegan false? If the
former then what *is* the code doing? If you mean the latter then
what is the difference between 'a random record with vegan false' and
what you have asked for which is 'a random result with specific
option'?

I am doing my best to help but if you do not explain your requirement
carefully then it is difficult for me. Perhaps you should give a more
detailed example, with real values, of what you want to happen.

Colin