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:in `new_cp'' app/controllers/critical_processes_controller.rb:61:in `create'' app/controllers/critical_processes_controller.rb:60:in `create'' 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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:46 pm, Mo Al <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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:in `new_cp'' > app/controllers/critical_processes_controller.rb:61:in `create'' > app/controllers/critical_processes_controller.rb:60:in `create'' > > 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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 10, 2:46 pm, Mo Al <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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.idThis 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 Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.