xirukitepe
xirukitepe

Reputation: 1615

undefined method `each' in Rails 3.2

I've tried the same scenario in rails console

But why am I getting...

undefined method each for #<ProjectProcurementManagementPlan:0x007ff9ecda2148>

In my model, I have a callback that will update the form immediately

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

But keeps on getting undefined method 'each'

But whenever I remove the self.each loop...

I get 'stack level too deep' type of error.

Any workarounds will be appreciated.

EDIT

Okay, now i realize that I should stick to my old approach. And that is without 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

But i am getting stack level too deep error

I saw from the logs, that there's an endless loop.

(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 `'

Upvotes: 0

Views: 396

Answers (2)

Sergey Bolgov
Sergey Bolgov

Reputation: 806

My guess is:

stack level too deep occurs because update_attribute() calls check_app_on_update? again that calls update_attribute() that calls check_app_on_update? again ...

Need more information to be more confident ...

Upvotes: 3

Michael Durrant
Michael Durrant

Reputation: 96454

because self means 1 object and there is no .each method for 1 object, .each is for collections.

Upvotes: 0

Related Questions