0

I have method called check_field, purpose of this method is for checking is there multiple value when input form.
I was think maybe I can check with multiple array on rails. I found some code on this link to check duplicate value inside array. So, I implement that method (difference) on my model , but I got stuck when implement to my Model, the methot won't called.

NoMethodError (undefined method `difference' for ["vvvv", "vvvv", "xxxxxxxxx", "xxxxxxxxx"]:Array):

here my Code.

class Model < ApplicationRecord
  has_many :child_models, -> { where(active: true) }, :foreign_key => :model_id, :dependent => :destroy
  accepts_nested_attributes_for :child_models, :allow_destroy => false
  before_validation :check_field, :before => [:create]
    def check_field
      all_field = Array.new
      child_models.each do |data|
        all_field << data.field
      end

      uniq_field = all_field.uniq
      result = uniq_field - all_field.difference(uniq_field)
      errors.add(:field, "Field must be Different")
    end

    def difference(other)
      h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
      reject { |e| h[e] > 0 && h[e] -= 1 }
    end
end

the field that I want to validate is on child_models,

class ChildModel < ApplicationRecord    
    validates :field, presence: { message: "Field" + I18n.t("sys_err_is_required"), :if => :active?, :scope => [:active] }, :uniqueness => { :message => 'Field is already exist', :if => :active?, :scope => [:active], :case_sensitive => false }
end

I was try to put check_field method on ChildModel, it didn't work, because the parameters can be read on Model class.

so then any solution for this case? thanks.

Ari
  • 27
  • 8

1 Answers1

0

you should use self.difference to call your method, because it is a classmethod. see https://dev.to/adamlombard/ruby-class-methods-vs-instance-methods-4aje

...
result = self.difference(uniq_field)
Oliver Gaida
  • 1,722
  • 7
  • 14
  • hi @Oliver Gaida , I was add `self.difference`, but still didnt work. it still raise same error.. `NoMethodError (undefined method `difference' for ["vvvv", "vvvv", "xxxxxxxxx", "xxxxxxxxx"]:Array):` – Ari Oct 07 '22 at 01:26
  • hi @Oliver Gaida to make sure my purpose, I mean I want detect the array result like this by implementing that difference method.. `a = [1,3,2,4,3,4]` `u = a.uniq #=> [1, 2, 3, 4]` `u - a.difference(u) #=> [1, 2]` so on my code should be like this. `result = uniq_field - all_field.difference(uniq_field)` but when I impement this method, it raise that erorr. – Ari Oct 07 '22 at 01:52
  • How do you call your Model, please edit your question and show the code snippet which is calling your Model. – Oliver Gaida Oct 07 '22 at 14:13
  • I think my question is clear. the error raised when create data, because I was set `before_validation :check_field` on Model. All I want to ask is how to call **difference()** method like was explained on this link https://stackoverflow.com/a/24989593/10232170 because when I implement this `uniq_field - all_field.difference(uniq_field)` , it didnt work. – Ari Oct 11 '22 at 04:35
  • it's up to you. I can't help without the source code. Good luck then! – Oliver Gaida Oct 11 '22 at 05:22
  • Hello sir @Oliver Gaida, I was add some code, and some explanation. Maybe you have an solution about my case? because I dont know what code that I must add again to explain.. – Ari Oct 11 '22 at 09:48