I am experiencing a loop within the after_validation callback when
trying to update some model attributes. However, it 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 flag but
with no success.
Why is this occurring and how how to make populate_address execute just
once and not call validation.
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