jeudi 18 février 2016

Stack level too deep (System Stack Error)

I have a message architecture where the messages are itself nested for one level. Each message (parent) can have many children and each message (children) belongs to parent.

The parent_id attributes identifies if a message is a parent/child and also whose child it is.

Please find below my model and the corresponding associations.

Class AbstractMessage

  belongs_to :parent, class_name: 'AbstractMessage', inverse_of: :children    

  scope :with_a_parent, where("parent_id IS NOT NULL")

  has_many :children, class_name: 'AbstractMessage', foreign_key: :parent_id, 
  include: [:children],
  conditions: "id IN (#{AbstractMessage.with_a_parent.select(:id).to_sql})",
  inverse_of: :parent

end

Here in this case, I am facing an infinite recursion loop with the following query.

AbstractMessage.where(id: message_root_ids).includes(:children).index_by(&:id)

The strange part is that this issue happens only when it is being called via web server (passenger/nginx). The same query when fired from console returns the proper results.

Any help on why this issue occurs. Also why it happens only through web server.

Thanks in advance for the help.

Aucun commentaire:

Enregistrer un commentaire