how do you compare a param to every item in an array in rails?
I have a codes model and I basically have a form which should authenticate simply to every code recorded so my controller is:
def cancel_sale
@codes = Code.find(:all)
@codes.each do |c|
@code = c.name
end
if param开发者_如何学Pythons[:auth] && params[:auth] == @code
something
else
@mensaje_de_salida = "wrong auth code"
end
end
I know it's bad, at this moment it only authenticates to the last recorded code.
What about this?
def cancel_sale
@codes = Code.find(:all)
if params[:auth] && @codes.include?(params[:auth])
#something
else
@mensaje_de_salida = "wrong auth code"
end
end
Try this. Gabe's method is correct as well if you actually want to use @codes for another purpose otherwise no need to retreive all the codes.
@codes = lambda { Code.find(:all, :conditions => ["auth = ?", params[:auth] ] ) }
if params[:auth] && !(@codes.call.empty?)
#something
else
#nothing
end
精彩评论