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 <<<< ----
You may be interested in looking at
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. [email protected]:
Paolo N. wrote:
You may be interested in looking at
Peak Obsession
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. [email protected]:
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
M. R. wrote:
Paolo N. wrote:
You may be interested in looking at
Peak Obsession
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. [email protected]:
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?
On 8/9/06, M. R. [email protected] 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
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
hth
–
Richard L.
From: Peak Obsession
“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. [email protected]:
after_initialize might help you
See ‘The after_find and after_initialize exceptions’ at
http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html
Chris