mysql - Check row exist or not in Ruby on Rails -


i new ruby on rails , have basic knowledge of mysql. using mysql db. question -- how check if row exists or not in table. have tried code it's not going straight else block, not if block:

@tasks = user.find_by("user_name = ? , password = ?", params[:user_name], params[:password]) if @tasks   redirect_to action: 'index', status: 302 else   redirect_to action: 'detail', status: 302 end 

if want find if user given name , password exists using ruby on rails, can this:

user.where(user_name: params[:user_name], password: params[:password]).exists? 

see railsguides: existence of objects.

the cause of original problem?

so code original poster submitted:

user.find_by("user_name = ? , password = ?", "#{params[:user_name]}", "#{params[:password]}") 

i removed string interpolation because unnecessary

user.find_by("user_name = ? , password = ?", params[:user_name], params[:password]) 

and apparently fixed problem. i'm not sure why make difference though, interpolated string should same value values in params dictionary.


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

php - Redirect and hide target URL -