Reputation: 125
Model Cafe
has meal_available
, which has boolean
type.
When searching cafe, I want to re-search by category checkbox:
<form action="/cafe/cafesearch" method="GET">
<label for="chk1"><input type="checkbox" id="chk1" name="meal_available" value=true>meal_available</label>
<input type="submit" value="submit">
</form>
At view, pass checkbox value through form tag, and at controller, I tried like this:
coffee = Cafe.all
if params[:meal_available] == true
@cafe = coffee.delete_if{|x| x.meal_available == false}
end
It didn't work. How can I delete element by model column in array?
Upvotes: 0
Views: 443
Reputation: 13487
You can use ActiveRecord
methods for it only:
if params[:meal_available] == true
@cafe = Cafe.where(meal_available: false)
end
In this case coffee = Cafe.all
is redundant.
coffee = Cafe.all
if params[:meal_available] == true
@cafe = coffee.select { |x| x.meal_available == false }
end
Upvotes: 0
Reputation: 601
You can just put in a scope in Cafe model to get the cafes with meal_available, more information on ActiveRecord scopes here
In cafe model :
class Cafe < ActiveRecord::Base
scope :meals_available, -> { where(meal_available: true) }
end
In controller :
@cafes = Cafe.meals_available
thats it, thanks.
Upvotes: 1