3

I am new to Ruby on Rails and I have basic knowledge of mysql. I am using MySQL db. My question is -- how to check if a row is exists or not in a table. I have tried this code but it's not going straight to the else block, not the if block:

@tasks = User.find_by("user_name = ? AND password = ?", params[:user_name], params[:password]) if @tasks redirect_to action: 'index', status: 302 else redirect_to action: 'detail', status: 302 end 
16
  • In the future, if something doesn't work as expected, please explain in what way that it's not working, like if you're getting any error messages, or what the response is, even if it's not the right one. Commented Apr 8, 2014 at 5:23
  • Try to find the users with unique fields. Commented Apr 8, 2014 at 5:23
  • 1
    #cupcake--I am not getting any error msg. It is going directly into else condition Commented Apr 8, 2014 at 5:25
  • @Bharatsoni .. I did't get u Commented Apr 8, 2014 at 5:27
  • 1
    @Cupcake OP meant the removal of string interpolation in the find_by Commented Apr 8, 2014 at 5:58

3 Answers 3

2

If you want to find if a user with the given name and password exists using Ruby on Rails, then you can do this:

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

See the RailsGuides: Existence of Objects.

The Cause of the Original Problem?

So this it the code that the original poster originally submitted:

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

I removed the string interpolation because it was unnecessary

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

and apparently that fixed the problem. I'm not sure why it would make a difference though, the interpolated string should be the same value as the values in the params dictionary.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks it is working.. But whatever i have written is it wrong?
@D'Scolz I can't actually tell, what you had originally looks like it should work too.
@D'Scolz there is no find_by in rails.
@akshay actually, there is.
@Cupcake please help me on Twitter-bootstrap-rails
2

You can use any of these solutions depending on your requirement.

Sol-1:

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

Sol-2:

User.find_by_user_name_and_password(params[:user_name], params[:password]) 
  • where returns an ActiveRecord::Relation (not an array, even though it behaves much like one), which is a collection of model objects. If nothing matches the conditions, it simply returns an empty relation.

  • find (and its related dynamic find_by_columnname methods) returns a single model object, or possibly a collection of model objects in an Array (not a Relation). If nothing is found, an ActiveRecord::RecordNotFound exception is raised.

So yes, if you only want and expect a single object, using find is easier, as otherwise you must call Model.where.first.

2 Comments

that means ... suppose i have 2 rows in table and both row have same data. in this case if i use where then i will get both the values
Accept one of the answers which you felt helped you most. That would help other users.
1

you can try it as well..

User.find_by_user_name_and_password(params[:user_name], params[:password])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.