Rails 3.2中未定义的方法`each'

时间:2013-05-31 01:26:00

标签: ruby-on-rails

我在rails console

中尝试了相同的方案

但为什么我得到......

each

的未定义方法#<ProjectProcurementManagementPlan:0x007ff9ecda2148>

在我的模型中,我有一个将立即更新表单的回调

after_update :check_app_on_update?

def check_app_on_update?

      self.each do |ppmp|
        ppmp_year = ppmp.year
        get_app = AnnualProcurementPlan.where(year: ppmp_year)
        get_id = get_app.map{|a| a.id }
        get_id.each do |app_id|
          update_attribute(:annual_procurement_plan_id, app_id)
        end
      end  
    end

但继续获得undefined method 'each'

但每当我删除self.each循环......

我收到'stack level too deep'类错误。

任何变通办法都将受到赞赏。

修改

好的,现在我意识到我应该坚持我的旧方法。那没有each

def check_app_on_update?
      ppmp_year = self.year
      get_app = AnnualProcurementPlan.where(year: ppmp_year)
      get_id = get_app.map{|a| a.id }

      get_id.each do |app_id|
        # ppmp = ProjectProcurementManagementPlan.last    
        #         ppmp.update_attribute(:annual_procurement_plan_id, app_id )
        @idd = app_id
      end
        update_attribute(:annual_procurement_plan_id, @idd )
  end

但我收到stack level too deep错误

我从日志中看到,这是一个无限循环。

(0.5ms)  UPDATE "project_procurement_management_plans" SET "status" = 'Approved', "updated_at" = '2013-05-31 09:55:00.000000', "annual_procurement_plan_id" = 1 WHERE "project_procurement_management_plans"."id" = 19
  AnnualProcurementPlan Load (0.5ms)  SELECT "annual_procurement_plans".* FROM "annual_procurement_plans" WHERE "annual_procurement_plans"."year" = 2012
   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 16737ms

SystemStackError - stack level too deep:
  (gem) actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:70:in `'

2 个答案:

答案 0 :(得分:3)

我的猜测是:

发生

stack level too deep是因为update_attribute()再次调用check_app_on_update?,调用再次调用update_attribute()的{​​{1}} ...

需要更多信息才能更自信......

答案 1 :(得分:0)

因为self表示1个对象,而1个对象没有.each方法,.each表示集合。