Help us understand the problem. What is going on with this article?

【Rails】カスタムバリデータ JSON そのほかのハマりどころ

データ

イケテナイかもしれないが、このようなデータを扱うとして、
typeを存在をバリデーションチェックしたい

sample_product
{ "type": "paper", "price": "4000"}

モデル

app/models/sample.rb
require_dependency '../validators/sample_product_validator.rb'

class Samplle < ApplicationRecord
  validates :sample_product,
    sample_product: true
end

バリデータ

app/validators/sample_product_validator.rb
class SampleProductValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if value.fetch("type").blank?
      record.errors.add(attribute, "を選択してください")
    end
  end
end

上記の場合、typeが入っていないデータがきた場合、バリデーターで

No Method Error

が起こるので

app/models/sample.rb
require_dependency '../validators/sample_product_validator.rb'

class Samplle < ApplicationRecord
  validates :sample_product,
    sample_product: true,
    presence: true
end

とする

binding.pryでカスタムバリデータの確認

class SampleValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    binding.pry
    puts record
    puts attribute
    puts value
  end
end

record => インスタンス変数に格納されている属性

[1] pry(#<SampleValidator>)> record
=> #<Sample:0x000055c3c48d1a48
 id: 2,
 team_id: 1,
 from: Tue, 22 Jan 2019,
 to: Sat, 26 Jan 2019,
 created_at: Mon, 21 Jan 2019 08:53:57 UTC +00:00,
 updated_at: Tue, 22 Jan 2019 04:52:34 UTC +00:00>

attribute => 検証される属性

[2] pry(#<SampleValidator>)> attribute
=> :from

value => 検証される属性の値

[3] pry(#<TwSurveyPeriodValidator>)> value
=> Tue, 22 Jan 2019

参考サイト

Rails 3, Unknown validator: 'EmailValidator'
Railsでカスタムバリデータのエラー解決方法を教えてください。
Rails tips: 知らないと損する4つのバリデーションレベル(翻訳)
Rails 4 でバリデータを自作する(custom validator)

if

    validates :milk_time_1,
      presence: {
        if: proc { |d| d.count_milk > 0 },
        message: 'を選択してください'
      }

上記のコードだと、conut_milkがなかった場合、NoMethodエラーが起きてしまう。
cont_milkをnullOKにしているからだ

    validates :milk_time_1,
      presence: {
        if: proc { |d| d.count_milk > 0 unless d.count_milk.blank? },
        message: 'を選択してください'
      }

上記のように、conut_mlik合ってかつのようにすると良い

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away