after_validation stack level too deep

I am experiencing a loop within a after_validation when trying to update
some attributes. Looks like it is calling validation recursively again
and again and I am getting "stack level too deep" error message. I tried
using a skip_callbacks but with no success.

Why is this occurring and how to fix it?

require 'correios-cep'
class Address < ActiveRecord::Base
  attr_accessible :id, :street, :neighborhood, :city, :state, :zipcode,
:country, :is_primary, :user_id, :latitude, :longitude,
:is_accurate_points
  attr_accessor :invalid_primary, :populated_address, :skip_callbacks
  belongs_to :user
  validates :street, presence: true
  validates :city, presence: true
  #validates :neighborhood, presence: true
  validates :state, presence: true
  validates :zipcode, presence: true
  validates :country, presence: true
  validates :is_primary, :inclusion => {:in => [true, false]}
  validate :is_primary, :invalid_primary?
  validate :street, :validateStreet?
  validate :zipcode, :validateZip?
  after_validation :populate_address

  def populate_address
     self.neighborhood = populate_address.neighborhood
     self.city = populate_address.city
     self.street = populate_address.street
     self.state = populate_address.state
  end
end

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"moURPhSZblDM6t70lg59TWUuDS8k6K/ygf8zMY1UBjE=",
"address"=>{"zipcode"=>"88025-100",
"street"=>"Rua Allan Kardec 35",
"neighborhood"=>"Agronômica",
"city"=>"Florianópolis",
"state"=>"SC",
"is_primary"=>"true"},
"commit"=>"Atualizar",
"id"=>"2"}
Toggle session dump
Toggle env dump
Response

Headers:

None

I am experiencing a loop within a after_validation when trying to update
some attributes. Looks like it is calling validation recursively again
and again and I am getting "stack level too deep" error message. I tried
using a skip_callbacks but with no success.

Why is this occurring and how to fix it?

require 'correios-cep'
class Address < ActiveRecord::Base
attr_accessible :id, :street, :neighborhood, :city, :state, :zipcode,
:country, :is_primary, :user_id, :latitude, :longitude,
:is_accurate_points
attr_accessor :invalid_primary, :populated_address, :skip_callbacks
belongs_to :user
validates :street, presence: true
validates :city, presence: true
#validates :neighborhood, presence: true
validates :state, presence: true
validates :zipcode, presence: true
validates :country, presence: true
validates :is_primary, :inclusion => {:in => [true, false]}
validate :is_primary, :invalid_primary?
validate :street, :validateStreet?
validate :zipcode, :validateZip?
after_validation :populate_address

def populate_address
    self.neighborhood = populate_address.neighborhood
    self.city = populate_address.city
    self.street = populate_address.street
    self.state = populate_address.state
end
end

If your method is called populate_address, and you have populate_address on the second line of that method, what exactly do you expect to happen but a loop? Is that a typo? You've used it over and over, and it doesn't seem to be defined anywhere else.

Walter

I am experiencing a loop within a after_validation when trying to update
some attributes. Looks like it is calling validation recursively again
and again and I am getting "stack level too deep" error message. I tried
using a skip_callbacks but with no success.

Why is this occurring and how to fix it?

require 'correios-cep'
class Address < ActiveRecord::Base
attr_accessible :id, :street, :neighborhood, :city, :state, :zipcode,
:country, :is_primary, :user_id, :latitude, :longitude,
:is_accurate_points
attr_accessor :invalid_primary, :populated_address, :skip_callbacks
belongs_to :user
validates :street, presence: true
validates :city, presence: true
#validates :neighborhood, presence: true
validates :state, presence: true
validates :zipcode, presence: true
validates :country, presence: true
validates :is_primary, :inclusion => {:in => [true, false]}
validate :is_primary, :invalid_primary?
validate :street, :validateStreet?
validate :zipcode, :validateZip?
after_validation :populate_address

def populate_address
   self.neighborhood = populate_address.neighborhood
   self.city = populate_address.city
   self.street = populate_address.street
   self.state = populate_address.state
end
end

If your method is called populate_address, and you have populate_address on the second line of that method, what exactly do you expect to happen but a loop? Is that a typo? You've used it over and over, and it doesn't seem to be defined anywhere else.

1.9.3-p484 :001 > def foo
1.9.3-p484 :002?> foo.bar
1.9.3-p484 :003?> end
=> nil
1.9.3-p484 :004 > foo
SystemStackError: stack level too deep
  from /Volumes/eddy/Users/waltd/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/irb/workspace.rb:80

Walter,

thanks for pointing this out. I think this is the problem, let me check
and I will get back.

Thank you

Walter Davis wrote in post #1150092:

Thank you. It was typo, fixed now.