LoginSignup
1
0

More than 1 year has passed since last update.

RubocopでRails/Presence: Useと指摘を受ける

Posted at

RuboCopで下記の指摘を受けました。

Rails/Presence: Use self.カラム名.presence || "hogehoge" instead of self.カラム名.present? ? self.カラム名: "hogehoge".
# 指摘を受けたソース
self.カラム名.present? ? self.カラム名: "hogehoge"

どうやら、presentメソッドではなく、
presenceメソッドというものを使いなさいと指摘されているようです。

下記記事によると、
presenceメソッドは、
オブジェクトが存在すればそのオブジェクトを返し、
存在しなければnilを返したい場合にとても便利なショートカット
…とのこと。

カラムが存在しない場合は、"hogehoge"という文言を返したいので、
下記ソースに修正。
二回カラム名を書く以前のソースよりシンプルになりました。

self.カラム名.presence || "hogehoge" 
1
0
0

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
0