Reputation: 3659
In my controller I have:
def show
@entry = Entry.find(params[:id])
@comments = @entry.comments
end
Now in the view:
<% if [email protected]? %>
<% @comments.each do |comment| %>
<%= comment.id %>
<% end %>
<% end %>
I know @comments
is not empty, but here I get @comments.empty? == true
When I add <%= @comments %>
before if
statement, all works fine. empty?
is false
, and I get comment ids. It looks like query to get comments is run when I add this new line? Any idea why and how can I make it work without this additional line?
Upvotes: 0
Views: 49
Reputation: 7622
Never use if !
. Instead use unless
.
Here you don't even need a empty check statement at all. .each
iterate only when there is some values.
Upvotes: 1
Reputation: 5437
Why do you need this statement at all?
This will work
<% @comments.each do |comment| %>
<%= comment.id %>
<% end %>
Upvotes: 4