首页 > 解决方案 > 为什么 ruby​​ 期望参数数量错误

问题描述

我有一个 Rails 应用程序,我在其中设置了一个方法 before_save activeRecord 回调,如下所示:

class GroupEvent < ApplicationRecord
    enum status: [ :published, :draft ]
    before_save :calculate_and_set_dates
    def calculate_and_set_dates
        missing_properties = []
           #check for available or set attributes 
           puts  "sss" + self.startDate.to_s
             if !self.startDate 
                   missing_properties<<"startDate" 
             end
             if !self.duration 
                missing_properties<<"duration" 
          end
           if !self.endDate
            missing_properties<<"endDate" 
            end
           
            binding.pry

           if  missing_properties.length<=1
             set_missing_property(missing_properties[0]) 
             else
                set_errors_for(missing_properties)
             end

  end

    private
    def set_missing_property(missing_property)
        case  missing_property
         when "startDate"
                 self.startDate = self.endDate - self.duration
         when "duration"
            self.duration = self.endDate -  self.startDate
         when  "endDate"
             self.endDate=  self.startDate +self.duration
        
       end
    end

结尾

注意:这个注意完整的类,所以不要担心 set_errors_for 方法的实现。

现在,我使用GroupEvent.create(name:"hackaton",description:"hecking my life away",startDate: DateTime.now, duration:10). 在set_missing_property(missing_properties[0])调用点时,我收到错误数量的参数错误:

ArgumentError: wrong number of arguments (given 1, expected 0) from /Users/haroonAzhar/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/attribute_methods/read.rb:15:in startDate'

如您所见,该方法set_missing_property在定义中有 1 个参数,为什么它期望为 0?更令人困惑的是:当我没有向 'set_missing_property' 方法传递任何参数时,我得到了这个错误:

ArgumentError: wrong number of arguments (given 0, expected 1) from /Users/haroonAzhar/Desktop/develop/whitespectre/app/models/group_event.rb:29:in set_missing_property'

我不知道为什么它在看

/Users/haroonAzhar/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/attribute_methods/read.rb:15:in startDate'

当我给出一个参数/参数但我检查了它所建议的文件时,它所指的部分如下所示:

module ClassMethods # :nodoc:
    private
      def define_method_attribute(name)
        ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
          generated_attribute_methods, name
        ) do |temp_method_name, attr_name_expr|
          generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{temp_method_name}
              name = #{attr_name_expr}
              _read_attribute(name) { |n| missing_attribute(n, caller) }
            end
          RUBY
        end
      end
  end

当定义的方法在类中正确时,甚至在那个地方寻找什么?但我需要回答的真正问题是为什么它期望错误的参数数量以及如何解决它?

提前感谢您的帮助,非常感谢:D

标签: ruby-on-railsrubyactiverecordrubygems

解决方案


好吧伙计们,我解决了(也许)

因此,endDate在我的情况下set_missing_property,我将其更改为:

when  "endDate"
         self.endDate =  self.startDate + self.duration

我相信我用一个不期望任何参数调用 self.startDate,所以它抛出了那个错误。

感谢所有花时间做这件事的偷窥者。真的很感激。


推荐阅读