How to get an array from a database in Rails3 even when there's only one record?
Posted
by
yuval
on Stack Overflow
See other posts from Stack Overflow
or by yuval
Published on 2011-01-02T01:36:47Z
Indexed on
2011/01/02
1:54 UTC
Read the original article
Hit count: 494
ruby-on-rails
|ruby-on-rails3
In Rails3, I have the following line:
@messages = Message.where("recipient_deleted = ?", false).find_by_recipient_id(@user.id)
In my view, I loop through @messages
and print out each message, as such:
<% for message in @messages %>
<%= message.sender_id %>
<%= message.created_at %>
<%= message.body %>
<% end %>
This works flawlessly when there are several messages.
The problem is that when I have one message, I get an error thrown at me:
undefined method
each'`
How do I force rails to always return an array of messages even if there's only one message so that each
always works?
Thanks!
© Stack Overflow or respective owner