How do I put logic in a View a scope or method in a Model?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-06-04T05:32:08Z Indexed on 2010/06/06 8:42 UTC
Read the original article Hit count: 219

Filed under:
|
|
|

I have the following in the view:

            <% unless contact_email.statuses.empty?%>
                (<%= contact_email.statuses.find(:last).status%>) 
             <% end %>

contact_email is an instance of a specific model.

Could I do something like this?

class ContactEmail < ActiveRecord::Base
  attr_accessible :contact_id, :email_id, :status, :subject, :body, :date_created, :date_sent

  def status
    unless contact_email.statuses.empty?
      contact_email.statuses.find(:last).status
    end
  end

end

is there a better way to do this? is there a way to use the || operator for a default if empty?

Basically, I would like to be able to do the following in the View:

<%= contact_email.status =>

IF there is a value, then display it, if not, show nothing.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby