Has anyone run into this problem?
I have a collection of comments that I loop through in the view as normal:
<% for comment in @post.comments %>
<%= comment.body %>
<% end %>
But I also have a form to add a comment, but it seems that if I use @post.comments.build instead of Comment.new in the controller, that it created a blank instance of a ‘comment’ in the loop.
I would prefer to use .build not .new
Has anyone encountered this? Is there a hack?
Thanks
Funny you mention, I encountered this a few days ago.
I ended up going with
Model.newbut you could also try reloading the association after you callbuild.@post.comments(true)will reload it. (You may also write@post.comments(:force_reload)for readability.)An alternative to reloading might be calling the
allnamed scope for the association, so@post.comments.all.I think both will issue a new query anyway, but maybe they’ll hit query cache.