# Change Bit Value To Text When Viewed

**URL:** <https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978>\
**Category:** Rails\
**Created:** [February 27, 2006, 2:39am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978 "2006-02-27T02:39:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 2:39am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/1 "2006-02-27T02:39:03Z")

</div>

Hey All, very simple question for you folks 😉

I have a @commutes object that for each record there is a bit value set  
(0/1) to denote if an accident occurred. What I want to do is have it  
display “Yes” or “No” when I view the listing of commutes. I can get it  
to  
display the bit value just fine with \<%= commute.accident %\> but how can  
I  
get it so that when a record with a 0 comes up that it displays “No” and  
if  
a 1 “Yes”? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 2:48am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/2 "2006-02-27T02:48:07Z")

</div>

\<%= commute.accident ? “Yes” : “No” %\>

Michael

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 2:51am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/3 "2006-02-27T02:51:08Z")

</div>

Perfect! That’s exactly what I needed.

Ryan

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 9:13am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/4 "2006-02-27T09:13:21Z")

</div>

Maybe you can try the Acts\_as\_bitfield plugin

[http://wiki.rubyonrails.org/rails/pages/Acts+As+Bitfield+Plugin](http://wiki.rubyonrails.org/rails/pages/Acts+As+Bitfield+Plugin)

Cheers!

–  
Mathias Stjernstrom

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/5 "2006-02-27T19:04:46Z")

</div>

Michael T. wrote:

> \<%= commute.accident ? “Yes” : “No” %\>

Following on to this solution: how would I go about it if I wanted to  
encapsulate this logic in a method? E.g.:

\<%= commute.accident.YesNo %\>

Is it possible to define the method in such a way that it is available  
for every boolean attribute in my model?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 27, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/6 "2006-02-27T19:21:19Z")

</div>

Probably, and in that case you would open up the  
ActiveRecord::ConnectionAdapters::Column class and add in the method,  
for instance boolean\_to\_string. That may or may not be the right spot  
for it.

Generally what I see is that you would define a helper, and use that.  
For instance, in your helpers/application\_helper.rb file put in  
something like

def YesNo(value)  
value ? “Yes” : “No”  
end

Then in your view you do

\<%= YesNo(commute.accident) %\>

Now granted, I’m just learning this stuff, so there’s probably another  
way that is a much better approach.

Michael

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 28, 2006, 5:53am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/7 "2006-02-28T05:53:51Z")

</div>

Just to clarify:

\<%= commute.accident ? “Yes” : “No” %\>

Didn’t work for me, but the following did.

\<%= commute.accident == 1 ? “Yes” : “No” %\>

Thanks!

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 28, 2006, 6:03am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/8 "2006-02-28T06:03:01Z")

</div>

I would think that

\<%= commute.accident? ? “Yes” : “No” %\>

should have also worked

Craig

---

<div class="post-metadata">

**Author:** ![parrfolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@parrfolio](https://www.ruby-forum.com/u/parrfolio)\
**Post date:** [February 28, 2006, 8:08am UTC](https://www.ruby-forum.com/t/change-bit-value-to-text-when-viewed/73978/9 "2006-02-28T08:08:13Z")

</div>

You might want to handle true and false different from zero and nonzero.  
Rails translates bits to boolean TrueClass and FalseClass, so this code  
works for me:

```
# Translate boolean true/false to string 'Yes' / 'No' for 

```

human-friendly reading  
# of checkbox fields.  
def handle\_translations(field)  
case field  
when TrueClass, FalseClass  
(field) ? ‘Yes’ : ‘No’  
else  
field  
end  
end

Craig W. wrote:

> I would think that
> 
> \<%= commute.accident? ? “Yes” : “No” %\>
> 
> should have also worked
> 
> Craig
