Reputation: 391
How do I convert this find query method below to use the where method instead (just trying to better myself in rails)?
@deals = Deal.find(params[:id])
I've tried
@deals = Deal.where(deal_id: params[:id])
AND
@deals = Deal.where("deal_id = params[:id]")
But am not getting any results -- I'm receiving undefined method errors and an assortment of other errors. Any help?
Thanks
Upvotes: 0
Views: 201
Reputation: 222
@deal = Deal.find(params[:id])
equivalent
@deal = Deal.where(id: params[:id]).first
.find() will return an exception if record not exist.
.where().first will return nil if record not exist.
Upvotes: 1
Reputation: 2129
The safe approach is:
@deals = Deal.where("deal_id = ?", params[:id])
Upvotes: 2
Reputation: 396
You would make your query like so:
@deal = Deal.where(id: params[:id])
Upvotes: 1