Forum: Ruby on Rails Change Bit Value To Text When Viewed

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ryan P. (Guest)
on 2006-02-27 03:39
(Received via mailing list)
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
display the bit value just fine with <%= commute.accident %> but how can
get it so that when a record with a 0 comes up that it displays "No" and
a 1 "Yes"? Thanks in advance!
Michael T. (Guest)
on 2006-02-27 03:48
(Received via mailing list)
<%= commute.accident ? "Yes" : "No" %>

Ryan P. (Guest)
on 2006-02-27 03:51
(Received via mailing list)
Perfect! That's exactly what I needed.

Mathias Stjernström (Guest)
on 2006-02-27 10:13
(Received via mailing list)
Maybe you can try the Acts_as_bitfield plugin


Mathias Stjernstrom
Mike D (Guest)
on 2006-02-27 20:04
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?

Michael T. (Guest)
on 2006-02-27 20:21
(Received via mailing list)
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"

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.

Ryan P. (Guest)
on 2006-02-28 06:53
(Received via mailing list)
Just to clarify:

<%= commute.accident ? "Yes" : "No" %>

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

<%= commute.accident == 1 ? "Yes" : "No" %>

Craig W. (Guest)
on 2006-02-28 07:03
(Received via mailing list)
I would think that

<%= commute.accident? ? "Yes" : "No" %>

should have also worked

Steve R. (Guest)
on 2006-02-28 09:08
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'

Craig W. wrote:
> I would think that
> <%= commute.accident? ? "Yes" : "No" %>
> should have also worked
> Craig
This topic is locked and can not be replied to.