# Putting Some Logic in Accessors That Get DB Field Data

**URL:** <https://www.ruby-forum.com/t/putting-some-logic-in-accessors-that-get-db-field-data/83389>\
**Category:** Rails\
**Created:** [January 25, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/putting-some-logic-in-accessors-that-get-db-field-data/83389 "2007-01-25T18:45:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 25, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/putting-some-logic-in-accessors-that-get-db-field-data/83389/1 "2007-01-25T18:45:28Z")

</div>

Wow… I have no clue how to explain this. Excuse my vocabulary.

Their is one field in my DB that I want to test if it is null before  
someone accesses it and if it is null return zero. How to I overwrite  
the accessor for a field in my model so that I can test the value  
being returned and if it is null return zero, otherwise return what  
the value is?

Why am I doing this? Because unfortunately the data I am accessing is  
from legacy systems so I can’t make sure that all the information has  
been cleaned up before hand! Plus the bozzo who is writing the tool  
to populate my DB from legacy systems refuses to clean data before he  
puts it into my DB! Garbage in… garbage out! 🙂

Am I making sense? How would I better describe what I am trying to do?

Thanks peeps 🙂

John

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 25, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/putting-some-logic-in-accessors-that-get-db-field-data/83389/2 "2007-01-25T19:38:38Z")

</div>

> Their is one field in my DB that I want to test if it is null before  
> someone accesses it and if it is null return zero. How to I overwrite  
> the accessor for a field in my model so that I can test the value  
> being returned and if it is null return zero, otherwise return what  
> the value is?

In your model, create a method with the same name as the attribute.  
This overwrites the automagical one made by AR.

def my\_attr  
if @my\_attr.nil?  
0  
else  
@my\_attr  
end  
end

@my\_attr is still populated by Active Record, but you’re now  
controlling what happens when it’s retrieved outside the model.

You can also overwrite the assignment by making a method like

def my\_attr=(value)  
if value.nil?  
@my\_attr = 0  
else  
@my\_attr = value  
end  
end

The equal sign in the method definition denotes it’s the method for  
assignment.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 25, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/putting-some-logic-in-accessors-that-get-db-field-data/83389/3 "2007-01-25T20:21:53Z")

</div>

Sexy! 🙂

On 1/25/07, Jim L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def my\_attr  
> You can also overwrite the assignment by making a method like
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)
