LoginSignup
1
1

More than 5 years have passed since last update.

Tapを使って、Active Recordを操作する

Last updated at Posted at 2018-08-24

Tapを使わないwhere文

if params[:email].present?
 User.where(email: params[:email])
elsif params[:username].present?
 User.where(username: params[:username])
else
 User.all
end

なんかもっといいほうほうがある気がする :thinking:

Tapを使ったwhere文

User.tap do |u|
 if params[:email].present?
  break u.where(email: params[:email])
 end
end.tap do |u|
 if params[:username].present?
  break u.where(username: params[:username])
 end
end.tap do |u|
 u.all
end

変わらないですかね?
こっちの方がややDRY化した気がした。

1
1
11

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1