Building against a new problem

Has anyone faced this problem?

I have a bunch of comments that I go through in the view as usual:

<% 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, it has created an empty "comment" instance in the loop.

I would rather use .build not.new Has anyone come across this? Is there a hack?

thanks

0


source to share


2 answers


It's funny that you mentioned, I ran into this a few days ago.

I ended up with Model.new

, but you can also try reloading the association after calling build

.

@post.comments(true)

will reload it. (You can also write @post.comments(:force_reload)

for readability.)



An alternative to rebooting could be to call the named area all

for the connection, therefore @post.comments.all

.

I think both will issue a new request anyway, but maybe they'll end up in the request cache.

+1


source


@comments = @post.comments
@comment  = @post.comments.build

      



This will do it I think.

0


source







All Articles