ruby on rails - if a value from controller is nil, how do I display 0? -


i creating report looks number of emails sent during period of time:

i display in view follows:

<td><%= @emails_sent.size %></td> 

and generated in controller follows:

   @sent_emails = contactemail.all(:conditions => ['date_sent >= ? , date_sent <= ?', @monday, @friday]) 

but emails have been sent, makes nil, causes view bonk.

what way address "nil" when .find method comes nothing goes 0 instead of thinking 'nil?

when using @neutrino solution might give error (if select null) in view call

<%= @emails_sent.size %> 

reason if selection nil, returns '0' , in view expecting array

you have 2 options

1 - modify @neutrino code slightly

@sent_emails = contactemail.all(:conditions => ['date_sent >= ? , date_sent <= ?', @monday, @friday]) || [] 

** note [] instead of 0

2 - count sql , rid of .size in view

cheers

sameera

update - changed array.new [] #thanks @gertas


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -