# Model filter?

**URL:** <https://www.ruby-forum.com/t/model-filter/94616>\
**Category:** Rails\
**Created:** [May 9, 2006, 4:42am UTC](https://www.ruby-forum.com/t/model-filter/94616 "2006-05-09T04:42:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 9, 2006, 4:42am UTC](https://www.ruby-forum.com/t/model-filter/94616/1 "2006-05-09T04:42:35Z")

</div>

Hi All,

Is there a way to filter / modify the output of a model class before  
it’s returned to controller/view that’s calling it?

Example: I have a field phone\_number which I always want to preformat  
using the number\_to\_phone() helper…

Any help is appriciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 9, 2006, 7:32am UTC](https://www.ruby-forum.com/t/model-filter/94616/2 "2006-05-09T07:32:22Z")

</div>

I believe the best way would be to write a new method in your model.rb  
file.  
Call it something like phone\_number\_formatted or something and just use  
that new method (model.phone\_number\_formatted) instead of the  
rails-generated model.phone\_number method.

I could be wrong as I’m still learning too.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 9, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/model-filter/94616/3 "2006-05-09T15:35:15Z")

</div>

Randy wrote:

> I believe the best way would be to write a new method in your model.rb  
> file.  
> Call it something like phone\_number\_formatted or something and just use  
> that new method (model.phone\_number\_formatted) instead of the  
> rails-generated model.phone\_number method.
> 
> I could be wrong as I’m still learning too.

Yeah, I was kinda looking for some way to call that method always from  
the model after the data was loaded, much the same way you would with  
before\_validation, before\_save, after\_save etc.

Like an after\_load or something.

There’s an after\_filter, but I think that doesn’t apply to active  
record…

Haven’t found anything in the API docs that would do what I’m thinking,  
but still looking.

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 1:39am UTC](https://www.ruby-forum.com/t/model-filter/94616/4 "2006-05-10T01:39:45Z")

</div>

If you just want to format it as you go, then overwrite the getting  
attribute method that Active Record provides.

Ie if you column is phone\_number

def phone\_number  
a = self.phone\_number  
some formatting stuff on a  
…  
end

Whenever you call the @obj.phone\_number you will get the format that  
your  
after.

This should probably go into a helper tho or save it in the format that  
you  
want with a call to before\_save.

Cheers

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 2:13am UTC](https://www.ruby-forum.com/t/model-filter/94616/5 "2006-05-10T02:13:38Z")

</div>

That needs to be:  
def phone\_number  
number = read\_attribute(:phone\_number)  
number.do\_formatting\_stuff  
end

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 3:05am UTC](https://www.ruby-forum.com/t/model-filter/94616/6 "2006-05-10T03:05:48Z")

</div>

On 5/10/06, Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That needs to be:  
> def phone\_number  
> number = read\_attribute(:phone\_number)  
> number.do\_formatting\_stuff  
> end

Nice, so read\_attribute avoids the recursive road to hell I was  
experiencing  
on the weekend! Thanks Wilson.

cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 3:24am UTC](https://www.ruby-forum.com/t/model-filter/94616/7 "2006-05-10T03:24:04Z")

</div>

Wilson,

Thanx for the fixup.

I can’t find any documentation for the read\_attribute method on the doc  
site.

Any hints as to why you use that instead of just self.phone\_number

Cheers

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 4:25am UTC](https://www.ruby-forum.com/t/model-filter/94616/8 "2006-05-10T04:25:02Z")

</div>

Awesome. That worked great!

One thing that didn’t work though…

I have this in my addressbook model:

def phone  
format\_phone(read\_attribute(:phone), {:delimiter =\> “-”})  
end

format\_phone is just a copy of number\_to\_phone() helper since it’s not  
inherited by ActiveRecord.

If I do this: \<%=@addressbook.phone%\>  
the output is: 513-545-3333  
which is what is desired.

If I do this:  
\<%=text\_field ‘addressbook’, ‘phone’, :size =\> 10, :class =\> ‘input’%\>  
I get: 5135453333 as the value of the text field (no formatting)

Weird…?..

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 4:15am UTC](https://www.ruby-forum.com/t/model-filter/94616/9 "2006-05-10T04:15:25Z")

</div>

> > > end
> > 
> > Nice, so read\_attribute avoids the recursive road to hell I was experiencing  
> > on the weekend! Thanks Wilson.

On 5/9/06, Liquid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

read\_attribute and write\_attribute are the behind-the-scenes accessors  
for the attributes that ActiveRecord plucks from the database.  
Are they really not in the API docs? That seems odd. Oh well.

The main reason to use them is when you’re redefining/overriding a  
method with the same name.  
In the example above, self.phone\_number will call the phone number  
method you just defined… an endless loop.

read\_attribute lets you essentially delegate the work to ActiveRecord.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 7:42am UTC](https://www.ruby-forum.com/t/model-filter/94616/10 "2006-05-10T07:42:56Z")

</div>

On 5/10/06, Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 5/9/06, Liquid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Wilson,
> > 
> > Thanx for the fixup.  
> > read\_attribute and write\_attribute are the behind-the-scenes accessors  
> > for the attributes that ActiveRecord plucks from the database.  
> > Are they really not in the API docs? That seems odd. Oh well.

Just, checked. It’s there in the ActiveRecord::Base under the heading  
“Overwriting default accessors”. Thanks for the tip.

cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/model-filter/94616/11 "2006-05-10T15:52:28Z")

</div>

> Try adding:  
> alias\_method :phone\_before\_type\_cast, :phone  
> …below the definition of your phone method.
> 
> If that’s what it takes to make it work, that’s fairly irritating.

That worked great also, thanks.

Only problem is, I’m using before\_type\_cast to unformat the phone number  
before validating…

before\_validation :unformat\_phone

def unformat\_phone  
phone\_before\_type\_cast.gsub!(/\D/, “”)  
end

Thanks allot for your help. I’ve learned a bunch trying to get this to  
work. Guess I’ll just write out the phone number text field the old  
fashioned way… or write my own helper.

Thanks again,

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 10, 2006, 5:56am UTC](https://www.ruby-forum.com/t/model-filter/94616/12 "2006-05-10T05:56:37Z")

</div>

On 5/9/06, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> format\_phone is just a copy of number\_to\_phone() helper since it’s not  
> Weird…?..

I just walked through what text\_field does, and it ends up calling  
attribute\_name\_before\_type\_cast.

Try adding:  
alias\_method :phone\_before\_type\_cast, :phone  
…below the definition of your phone method.

If that’s what it takes to make it work, that’s fairly irritating.
