Stack level too deep problem

Hi

I am getting a Stack level too deep error when i try creating a new
critical process object:

here is the code:

class Role < ActiveRecord::Base
has_many :authorizations
has_many :critical_processes, :through => :authorizations
has_many :assignments
has_many :users, :through => :assignments
end

class CriticalProcess < ActiveRecord::Base
has_many :authorizations
has_many :roles, :through => :authorizations, :dependent => :destroy,
:primary_key => :cp_secondary_id

after_create :new_cp

def create_roles
self.roles.create :name => “#{self.cp_title} edit”,
:critical_process_id => self.id, :edit => true, :review => false
self.roles.create :name => “#{self.cp_title} review”,
:critical_process_id => self.id, :edit => false, :review => true
end

def set_secondary_id
self.update_attribute :cp_secondary_id, self.id
end

def new_cp
if self.cp_secondary_id.blank?
set_secondary_id
create_roles
end
end
end

here is the error from the log:

SystemStackError (stack level too deep):
app/models/critical_process.rb:17:in create_roles' app/models/critical_process.rb:31:innew_cp’
app/controllers/critical_processes_controller.rb:61:in create' app/controllers/critical_processes_controller.rb:60:increate’

The issue is arising when the “create_roles” method is getting called.
Anyone know how i can fix this problem, i am new to rails and web
development. what changes would i have to make?

Thank You

Your associations are incorrect. Both Role and CriticalProcess refer
to each other via has_many – one of them must use belongs_to. And
where is the Authorization class? What is it’s relationship to
CriticalProcess?

Fix the associations problem and the loop will likely go away.

Why? I suspect because the CriticalProcess has_many :roles implies
that Role belongs_to it. Thus, ActiveRecord creates its own
create_roles for you. See
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many.
Notice the methods you get for free with each kind of assocation.

Nothing will work the way you have it defined now.

Martin

On Mar 10, 2:46pm, Mo Al [email protected] wrote:

has_many :assignments
def create_roles
self.roles.create :name => “#{self.cp_title} edit”,
:critical_process_id => self.id, :edit => true, :review => false
self.roles.create :name => “#{self.cp_title} review”,
:critical_process_id => self.id, :edit => false, :review => true
end

def set_secondary_id
self.update_attribute :cp_secondary_id, self.id

This is the problem - internally, update_attribute calls save on the
Role object while it’s still in the first save transaction and
new_record? is still set. This triggers the after_create callback
again, and boosh - you’ve got a stack overflow.

I don’t think the :primary_key option is going to work on the :through
association, though; what does the Authorization join model look like,
and how are the three linked together?

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs