There actually are after_initialize and after_find hooks for AR. But you
have to define them, ie.
class Article
after_initialize :some_method # don't do this, some_method won't be
called
# do this instead
def after_initialize
self.some_field = "some value" if self.new_record?
end
end
I think the 'if self.new_record?' check might work to separate
initialization from finding.
On Feb 11, 2008 2:50 PM, Jordan Fowler <[EMAIL PROTECTED]> wrote:
> In that case, you'd want to overwrite the initialize method, but very
> carefully. Here's how I might do it:
>
> def initialize(*args)
> super(*args)
> # ... your code here
> end
>
> Jordan
>
>
>
>
> On 2/11/08, Glenn Little <[EMAIL PROTECTED]> wrote:
> >
> >
> > I think I was really looking for something that would act as an
> > initialization even if I wasn't necessarily saving the object to
> > the database. "after_initialize" seemed like the right thing until
> > I found the somewhat dramatic extra effect of overwriting fields
> > in find() results. I probably just need to overwrite new() or
> > something, but for now I'll just work around it.
> >
> > -glenn
> >
> > Jordan Fowler wrote:
> > > Glenn,
> > >
> > > I don't quite understand what you're asking for. When you call save on
> > > an ActiveRecord object, the following happens:
> > >
> > > * (-) save
> > > * (-) valid
> > > * (1) before_validation
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001303>
> > >
> > > * (2) before_validation_on_create
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001305>
> > >
> > > * (-) validate
> > > * (-) validate_on_create
> > > * (3) after_validation
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001304>
> > >
> > > * (4) after_validation_on_create
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001306>
> > >
> > > * (5) before_save
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001297>
> > >
> > > * (6) before_create
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001299>
> > >
> > > * (-) create
> > > * (7) after_create
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001300>
> > >
> > > * (8) after_save
> > > <
> > http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001298>
> > >
> > > You should be able to find what you're looking for.
> > >
> > > --Jordan
> > >
> > >
> > > On 2/11/08, *Glenn Little* <[EMAIL PROTECTED]
> > > <mailto:[EMAIL PROTECTED]>> wrote:
> > >
> > >
> > > I'm looking for a callback that I think is something like
> > > after_initialize and before_create. I'd like it to take
> > > effect whenever a new object is created (unlike before_create
> > which
> > > I believe only happens before saving to a database),
> > > but I do *not* want it affecting the results of, say MyClass.find.
> > >
> > > For instance, if in my_class.rb I define:
> > >
> > > def after_initialize
> > > self.some_field = "initial value"
> > > end
> > >
> > > then MyClass.find(<conditions>).some_field *always* equals
> > > "initial value", which is definitely not what I want.
> > >
> > > I'd like null values to remain null as well in finds, so putting
> > > in an "unless self.some_field" wouldn't help me.
> > >
> > > Thanks...
> > >
> > > -glenn
> > >
> > > Website: http://www.jordanfowler.com
> > > Phone: 406.546.8055
> > > >
> >
> >
> >
>
>
> --
> Jordan A. Fowler
> 2621 First Ave Apt 5
> San Diego, CA 92103
> E-mail: [EMAIL PROTECTED]
>
> Website: http://www.jordanfowler.com
> Phone: 406.546.8055
> >
>
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---