Astm
Astm

Reputation: 1720

Rails 4 search by 2 fields in database

how i can search by 2 field in Rails 4

I'm trying to use this commands but it all not work with me

@user=User.by_username_and_password(params[:username],params[:password])
session[:user_user][email protected]

Also trying ton use this

@user=User.where(:user=>params[:username]).where(:pass=>params[:password])
session[:user_name][email protected]

so how in Rails 4 i can search by 2 or 3 fields ???

Upvotes: 0

Views: 216

Answers (3)

Bharath Mg
Bharath Mg

Reputation: 1127

You can use this code.

 User.where("username = ? AND password = ?", params[:username], params[:password))

Upvotes: 0

BroiSatse
BroiSatse

Reputation: 44685

User.find_by(username: params[:username], password: params[:password])

Upvotes: 2

AnkitG
AnkitG

Reputation: 6568

You should use this way

User.where(username: params[:username],password: params[:password]).first

or

User.find_by(username: params[:username],password: params[:password])

Where username and password are the columns in your table.

Upvotes: 1

Related Questions