0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

with_optionの使い方

Last updated at Posted at 2020-10-10

##with_optionとは
同じオプションを使った記述が複数存在している場合、
それらを一つにまとめることができるもの。
記述量を少なくできることが利点。

##使い方
例 app/models/item.rbファイルに、以下の同じオプションを使っているコードが複数あるとする。

validates :category_id, numericality: { other_than: 1 }
validates :days_to_send_id, numericality: { other_than: 1 }
validates :item_condition_id, numericality: { other_than: 1 }
validates :prace_id, numericality: { other_than: 1 }
validates :fee_id, numericality: { other_than: 1 }

この時、with_optionを使えば、以下のように記述量を少なく、みやすいコードにすることができる。

with_option numericality: { other_than:1} do
 validates :category_id
 validates :days_to_send
 validates :item_condition_id
 validates :prace_id
 validates :fee_id
end

以上になります。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?