Martin...
It would be more helpful to me if you were to actually describe what
you are talking about.
If what you are saying is "override initialize" like so:
<pre>
Class Recipe < ActiveRecord::Base
def initialize (name, yield_amount, yield_unit)
self.unit_type = get_unit_type(yield_unit)
self.name = name
self.yield_amount = yield_amount
self.yield_unit = yield_unit
end
end
<pre/>
that's not a good idea. All the posts I have read recommend against
this because Active Record doesn't always use initialize when creating
new instances.
If what you are saying is "add parameters to new, like so (or with a
hash):
<pre>
user = User.new(:name => "David", :occupation => "Code Artist")
<pre/>
that's exactly what I want to avoid having to do every time I create a
new instance.
If there's another approach that I don't know about, I'm all ears.
Thanks...jon
On Sep 13, 8:45 pm, Marnen Laibow-Koser <[email protected]> wrote:
> Jon Seidel wrote:
> > I've done all that;
>
> Done all what? You failed to quote what you were referring to.
>
> > but since this is an ActiveRecord object, I was
> > hoping there was a way to do this in one spot,
>
> There is. Use the constructor as I already suggested. Do you not know
> how to do this?
>
> > through a Rails hook,
> > rather than having to remember to code this each time.
>
> Right. That's what constructors do.
>
>
>
> > The way I encapsulated all this was to create an initialize_to_zeros
> > method and now I call that each time after doing a model.new
>
> Unnecessary. Just put that in the constructor.
>
>
>
> > ...jon
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.