# Function before\_save

**URL:** <https://www.ruby-forum.com/t/function-before-save/102016>\
**Category:** Rails\
**Created:** [August 9, 2006, 9:35am UTC](https://www.ruby-forum.com/t/function-before-save/102016 "2006-08-09T09:35:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 9, 2006, 9:35am UTC](https://www.ruby-forum.com/t/function-before-save/102016/1 "2006-08-09T09:35:40Z")

</div>

Hi everybody

I would like a function as the “before\_save” method in the model. But it  
must be the opposite. When I take out data from the database through the  
model, I want to call a function before the data are available in the  
controller.

Can anyone help me?

---- \>\>\>\> thx \<\<\<\< ----

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 9, 2006, 10:46am UTC](https://www.ruby-forum.com/t/function-before-save/102016/2 "2006-08-09T10:46:27Z")

</div>

You may be interested in looking at

[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

where all the possible callbacks are documented. “after\_find” or  
“after\_initialize” may do what you are looking for. Anyway carefully  
read the caveats for these two since they have significant impact on  
your application performance.

Paolo

2006/8/9, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 9, 2006, 11:01am UTC](https://www.ruby-forum.com/t/function-before-save/102016/3 "2006-08-09T11:01:57Z")

</div>

Paolo N. wrote:

> You may be interested in looking at
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)
> 
> where all the possible callbacks are documented. “after\_find” or  
> “after\_initialize” may do what you are looking for. Anyway carefully  
> read the caveats for these two since they have significant impact on  
> your application performance.
> 
> Paolo
> 
> 2006/8/9, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

That’s ok - I think after\_find is my favorite.

after\_find :delete\_type  
def delete\_type  
if self.name =~ /(\_f|\_m)$/  
(self.name).sub!(/(\_f|\_m)+$/, ‘’)  
end  
end

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 9, 2006, 11:06am UTC](https://www.ruby-forum.com/t/function-before-save/102016/4 "2006-08-09T11:06:07Z")

</div>

M. R. wrote:

> Paolo N. wrote:
> 
> > You may be interested in looking at
> > 
> > [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)
> > 
> > where all the possible callbacks are documented. “after\_find” or  
> > “after\_initialize” may do what you are looking for. Anyway carefully  
> > read the caveats for these two since they have significant impact on  
> > your application performance.
> > 
> > Paolo
> > 
> > 2006/8/9, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

That’s ok - I think after\_find is my favorite.

after\_find :delete\_type  
def delete\_type  
if self.name =~ /(\_f|\_m)$/  
(self.name).sub!(/(\_f|\_m)+$/, ‘’)  
end  
end

So I save a username into the database, I add the extension \_m (male) or  
\_f (female) to the username (for example: miller\_f oder miller\_m). When  
I take out the data from the model, I want to delete this extension \_m  
or \_f so I only see the name “miller” without the extension.

Why doesn’t run this code?

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 15, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/function-before-save/102016/5 "2006-08-15T14:10:39Z")

</div>

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

> > >
> 
> ```
> (self.name).sub!(/(_f|_m)+$/, '')
> end
> 
> ```
> 
> end
> 
> So I save a username into the database, I add the extension \_m (male) or  
> \_f (female) to the username (for example: miller\_f oder miller\_m). When  
> I take out the data from the model, I want to delete this extension \_m  
> or \_f so I only see the name “miller” without the extension.
> 
> Why doesn’t run this code?

Err, not sure why it doesn’t run; but I must ask why you are doing  
this in the first place. What’s wrong with a gender column containing  
either male or female (text or foreign key to another table) or single  
table inheritance if you need behaviour specific to the sexes?

Chris

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/function-before-save/102016/6 "2006-08-15T14:18:43Z")

</div>

M. R. wrote:

> So I save a username into the database, I add the extension \_m (male) or  
> \_f (female) to the username (for example: miller\_f oder miller\_m). When  
> I take out the data from the model, I want to delete this extension \_m  
> or \_f so I only see the name “miller” without the extension.
> 
> Why doesn’t run this code?

Aside from the obvious question of why you are not storing the m/f as a  
separate field, I’d do this by overriding the ‘name’ accessor. This has  
the advantage that it’s only going to execute the code when you need it.

def name  
n = read\_attribute(:name)  
n[0, n.size - 2]  
end

This assumes that every name has \_x on the end of it. Also, to only do  
this once per object, I’d cache the result - so this becomes:

def name  
@name ||= begin  
n = read\_attribute(:name)  
n[0, n.size - 2]  
end  
end

For more, take a look at ‘overwriting default accessors’:  
[http://rubyonrails.org/api/classes/ActiveRecord/Base.html](http://rubyonrails.org/api/classes/ActiveRecord/Base.html)

hth

–  
Richard L.

> **[Livsey.org](http://livsey.org)**
>
> I’ve been using Firebase with Ember.js quite a lot recently and have just
> released Fireplace, a library to integrate the two more easily. It

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 9, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/function-before-save/102016/7 "2006-08-09T16:52:51Z")

</div>

From: [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

“Unlike all the other callbacks, after\_find and after\_initialize will  
only be run if an explicit implementation is defined (def after\_find).  
In that case, all of the callback types will be called”

Anyway, if the kind of stuff you need is what you exposed I think that  
defining a method in the model class to obtain the clean name would  
be a nicer solution than using the callback, so you can just call  
@obj.cleanname when you need the stripped version of the name.

Paolo

2006/8/9, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/function-before-save/102016/8 "2006-08-15T14:19:20Z")

</div>

after\_initialize might help you

See ‘The after\_find and after\_initialize exceptions’ at  
[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

Chris
