Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 更新我的模型的方法';世界新纪录_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 更新我的模型的方法';世界新纪录

Ruby on rails 更新我的模型的方法';世界新纪录,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个名为UserPrice的模型,它有一个表单,您可以一次创建多个UserPrice。我有一个名为:all_dates的虚拟属性,它假设要更新我的UserPrice模型名为:purchase\u date的另一个日期选择字段,但由于它的工作不正确,我需要在此详细说明此方法,以便我可以让它工作: def save_all_dates_to_user_prices if !self.all_dates.nil? self.user_prices.each {|up| up.purchase_

我有一个名为
UserPrice
的模型,它有一个表单,您可以一次创建多个
UserPrice
。我有一个名为
:all_dates
的虚拟属性,它假设要更新我的
UserPrice
模型名为
:purchase\u date
的另一个日期选择字段,但由于它的工作不正确,我需要在此详细说明此方法,以便我可以让它工作:

def save_all_dates_to_user_prices
 if !self.all_dates.nil?
 self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
 end
end
我将开始:

  • 我定义了将所有日期保存到用户价格的方法 在我的UserPrice模型中保存之前

  • if!self.all_dates.nil?
    表示正在检查UserPrice.all_dates属性是否为空(nil?)

  • 这就是我迷路的地方;在这条线上不确定:

    self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
    
    • 它包装了每个UserPrice.user\u价格?如果我试图获得一系列新记录,它不会是这样的,对吗
    • 为什么要使用
      | up |
      ,这是
      self.user\u价格。每个
      代表什么
    • self.user\u prices.each
      =包装在
      {}
      中的任何东西(散列)
    除了回答我上面的问题外,是否有人可以为我填写/更正有关此方法的详细信息


    谢谢,我对Rails和Ruby还不熟悉,在编写代码的过程中尝试学习。

    每个
    都是类的一个方法,数组和哈希都实现了这个方法。它将块作为参数,并将其简单地应用于可枚举的所有元素。你问的那句话是这样翻译的:

    对于每个
    用户价格
    ,如果是新的
    用户价格

    up
    只是一个引用当前枚举元素的变量


    这里有一个。

    每个
    都是类的一个方法,数组和哈希都实现了这个方法。它将块作为参数,并将其简单地应用于可枚举的所有元素。你问的那句话是这样翻译的:

    对于每个
    用户价格
    ,如果是新的
    用户价格

    up
    只是一个引用当前枚举元素的变量


    这里有一个。

    这是一个有点笨重的Ruby代码,下面是它的一个分解,稍微重写一下:

    def save_all_dates_to_user_prices
      # If all_dates is defined...
      if (self.all_dates)
        # ...then for each entry in user_prices hereby called 'up'...
        self.user_prices.each do |up|
          # ...check if this is a new record...
          if (up.new_record?)
             # ...and assign the purchase_date
             up.purchase_date = self.all_dates
          end
        end
      end
    end
    

    如果您熟悉JavaScript,那么Ruby中的
    x.each{y}}
    与使用jQuery的JavaScript中的
    x.each(function(y){…})
    类似。在这两种情况下,竖条内的变量都表示该函数块的参数。

    这是一段有点笨重的Ruby代码,下面是对它的分解,稍微重写一下:

    def save_all_dates_to_user_prices
      # If all_dates is defined...
      if (self.all_dates)
        # ...then for each entry in user_prices hereby called 'up'...
        self.user_prices.each do |up|
          # ...check if this is a new record...
          if (up.new_record?)
             # ...and assign the purchase_date
             up.purchase_date = self.all_dates
          end
        end
      end
    end
    
    如果您熟悉JavaScript,那么Ruby中的
    x.each{y}}
    与使用jQuery的JavaScript中的
    x.each(function(y){…})
    类似。在这两种情况下,垂直条内的变量表示该函数块的参数