# Rails best practice to check an objectâ€™s e xistence before displaying an attribute in a layout

**URL:** <https://www.ruby-forum.com/t/rails-best-practice-to-check-an-objecta-s-e-xistence-before-displaying-an-attribute-in-a-layout/193028>\
**Category:** Rails\
**Created:** [August 10, 2010, 8:22am UTC](https://www.ruby-forum.com/t/rails-best-practice-to-check-an-objecta-s-e-xistence-before-displaying-an-attribute-in-a-layout/193028 "2010-08-10T08:22:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wtb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/53a042/32.png) [@wtb](https://www.ruby-forum.com/u/wtb)\
**Post date:** [August 10, 2010, 8:22am UTC](https://www.ruby-forum.com/t/rails-best-practice-to-check-an-objecta-s-e-xistence-before-displaying-an-attribute-in-a-layout/193028/1 "2010-08-10T08:22:25Z")

</div>

I have the following code in a layout:

Posted \<%=time\_ago\_in\_words post.created\_at %\> ago  
\<% if post.has\_tag != nil %\>  
in the \<%= post.get\_first\_tag.name %\> category  
\<% end %\>  
And the following code in the post model which is inheriting form  
ActiveRecord::Base

def has\_tag  
!self.tags.empty?  
end

def get\_first\_tag  
self.tags[0]  
end  
Tags is also inherited from ActiveRecord::Base and Post ‘has\_many’  
Tags

Firstly: Is this the best way of checking if the post object has at  
least 1 associate tag attribute.

Secondly: Should I be putting this logic into a helper method?

Thirdly: Why doesn’t the following work (it returns a # where the tags  
should be):

in the \<%= post.tags.to\_sentence %\> category,  
I guess its because tags aren’t actually stored as an array attribute,  
but i don’t really know.

---

<div class="post-metadata">

**Author:** ![wtb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/53a042/32.png) [@wtb](https://www.ruby-forum.com/u/wtb)\
**Post date:** [August 10, 2010, 12:47pm UTC](https://www.ruby-forum.com/t/rails-best-practice-to-check-an-objecta-s-e-xistence-before-displaying-an-attribute-in-a-layout/193028/2 "2010-08-10T12:47:37Z")

</div>

ActiveRecord already has helper methods defined, so adding wrappers  
like has\_tag and get\_first\_tag is usually not necessary, and only  
makes your code more complex.

1. You can do  
\<% if post.tags.any? %\>  
in the \<%= post.tags.first.name %\> category  
\<% end %\>  
And remove the get\_first\_tag and tags methods from your model.

2. This kind of logic is well suited for the view layer (i think).

3. Hard to tell without knowing what tags actually returns. Try using  
post.tags.inspect to see what it returns.  
I suspect you see ‘#’ because the objects in the array returned by  
tags doesn’t have a to\_s method defined.  
to\_sentence converts an array to a string using to\_s on each element.

If you have a Tag class, try adding this to that class:  
def to\_s  
name  
end

If you’re using a plugin/gem for the tags, you can either monkepatch  
it or do something like this:  
post.tags.map(&:name).to\_sentence (assuming Tag has a name attribute  
you want to display).  
This kind of logic is appropriate to put in a helper.

–  
gudleik

---

<div class="post-metadata">

**Author:** ![wtb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/53a042/32.png) [@wtb](https://www.ruby-forum.com/u/wtb)\
**Post date:** [August 10, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/rails-best-practice-to-check-an-objecta-s-e-xistence-before-displaying-an-attribute-in-a-layout/193028/3 "2010-08-10T17:09:24Z")

</div>

On Mon, Aug 9, 2010 at 19:23, wtb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<% if post.has\_tag != nil %\>  
> …  
> def has\_tag  
> !self.tags.empty?  
> end

false is not the same as nil. You’ve been hacking too much LISP. 🙂  
Dump the “!= nil”, leaving simply “if post.has\_tag”.

-Dave

–  
Specialization is for insects. -RAH | Have Pun, Will Babble! -me  
Programming Blog: [http://codosaur.us](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)

- 
  - 
    - 
      - 
        - WATCH THIS SPACE \* \* \* \* \* | Ruby: [http://mars.groupsite.com](http://mars.groupsite.com)
