# Display optional db data

**URL:** <https://www.ruby-forum.com/t/display-optional-db-data/186959>\
**Category:** Rails\
**Created:** [April 14, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959 "2010-04-14T23:13:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 14, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/1 "2010-04-14T23:13:50Z")

</div>

ive got a few models that may or may not have some data: urls / phone  
numbers / dates / times / etc  
whats the best way to test and format this data for displaying on a  
webpage?

the way im thinking of doing it would be a few lines of code to:

- test if not null
- format properly
- display in proper div / ul container

and then do that for each thing  
is there a cleaner way to do this?

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/2 "2010-04-15T20:09:18Z")

</div>

could someone perhaps advise on how to move some of this logic from  
view to model?  
like ‘get\_price\_info’ and it returns the properly formatted string or  
nothing?  
or should these tests be performed in the view?

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/3 "2010-04-15T20:18:40Z")

</div>

On Thu, Apr 15, 2010 at 11:05 AM, dan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> could someone perhaps advise on how to move some of this logic from  
> view to model?  
> like ‘get\_price\_info’ and it returns the properly formatted string or  
> nothing?  
> or should these tests be performed in the view?

I’m not sure what you’re looking for here; if you have a view with e.g.  
\<%= @thing.foo %\>  
and this particular “foo” is nil, nothing will display, otherwise you  
get  
the value of “foo”.

If that’s not the behavior you want, please describe in more detail.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 8:37pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/4 "2010-04-15T20:37:24Z")

</div>

if thing.foo is a date, price, url or something else that requires  
formatting, it requires more than =thing.foo  
and since you cant format nil, a test needs to be performed before  
trying to format said data  
my views are getting littered with these tests and formats, sometimes  
there nested:  
is there price data?  
is there an advance purchase price?  
is there an advance purchase expiry date?

its getting kinda ugly in my view  
whats the best way to handle this?

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/5 "2010-04-15T21:01:24Z")

</div>

On Thu, Apr 15, 2010 at 11:36 AM, dan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if thing.foo is a date, price, url or something else that requires  
> formatting, it requires more than =thing.foo  
> and since you cant format nil, a test needs to be performed before  
> trying to format said data  
> my views are getting littered with these tests and formats, sometimes  
> there nested:

Some possible approaches:

1. make the default in the database some value other than null.

2. \<%= @thing.foo.format rescue nil %\>

3. \<%= @thing.foo.nil? ? " N/A " : @thing.foo.format %\>

I’m sure someone will suggest others, but HTH 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/6 "2010-04-15T21:20:09Z")

</div>

The logic for this probably belong in view helpers and not in the  
model since its really about how the data is displayed and not about  
how the data behaves.

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 16, 2010, 10:32am UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/7 "2010-04-16T10:32:19Z")

</div>

It would be easier if you post the actual view code, and then somebody  
can show you how you might refactor it by moving logic into helpers

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 15, 2010, 10:14pm UTC](https://www.ruby-forum.com/t/display-optional-db-data/186959/8 "2010-04-15T22:14:00Z")

</div>

anyone have a good example of how best to move this logic to a helper?
