jeudi 9 mai 2019

Model.where() with multiple params with check if they are present in rails

I am new to rails. I started working on a POC.

My Controller code is below:

> @xyz = if params[:id].present?
>                   if params[:mobile_number].present?
>                     Seeker.where("id = ? and mobile_number = ?" , params[:id], params[:mobile_number])
>                     
>                   else
>                    
>                     Seeker.where("id = ?", params[:id])
>                 end
>               elsif params[:seekerid].present?
>                   Seeker.where("mobile_number = ?" , params[:mobile_number])
>               else
>                 Seeker.where(nil);
>               end

But I don't think so Its good approach. Consider If i have many parameter then Putting present? condition then forming query would become complex. or can i form a query separately then place it into where condition as instance variable. what is the better approach?

Aucun commentaire:

Enregistrer un commentaire