I have a comment table that is linked to itself for answers. Basically, a comment that has parent_id is also a response to its parent comment.
To do this, I use a recursive view, very simple, that worked in the past, but does not work with rails 3.2.0 and ruby 2.1.1
Here is the simplified code:
<% x = comment.replies %> <%= comment.id %>; <%= comment.class %><br/> <%= comment.replies.class %><br><br> <hr> <br><br> <% if x and x.is_a?(Array) %> <%= render :partial => "/_redesign/entry/comment", :collection => x, :as => :comment%> <% end %>
Exit:
349,223; A comment
Array
349,229; A comment
A comment
In the second iteration, comment.replies is a comment, not an array, and everything falls from there.
But if I change the first line and add a reboot:
<% x = comment.reload.replies %>
everything starts to work, output:
349,223; A comment
Array
349,229; A comment
Array
349230; A comment
Array
I would like to understand what is happening here, and how the association can return an instance of one object instead of its list and why it works with reloading.
Model code added:
class Comment < Response acts_as_deactivatable :dependencies => [:community_news_feed_items] has_many :replies, :class_name=>"Comment", :foreign_key=>"referring_c_id", :order=>"date ASC" belongs_to :parent_comment, :class_name=>"Comment", :foreign_key=>"referring_c_id" end