Forum: Ruby on Rails Overriding new or create for a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0fedb21065278a7bc1afe2491b78e40?d=identicon&s=25 Julian Gall (Guest)
on 2006-03-14 22:26
I have a model where I need to calculate one of the fields when I create
a new instance. It seems to me that I should do this in the model code
and that I should override the new and/or create methods.

Is this the right approach and, if so, how can I execute the existing
method but add on my calculation?

I want something like:

def create
  -Do the original create first-
  field = calculated_value
  save
  self
end

How do I call the original create method and use the parameters passed?
Also, should I be overriding new or create or both?

Thanks for any help.

Julian
188a96403bfb1b3781ab95fc1bc92241?d=identicon&s=25 Jordan Elver (Guest)
on 2006-03-15 00:12
(Received via mailing list)
> How do I call the original create method and use the parameters passed?
> Also, should I be overriding new or create or both?

I'm new at this, but I think you'll want to take a lot at callbacks.

http://api.rubyonrails.com/classes/ActiveRecord/Ca...
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-15 00:55
(Received via mailing list)
def test(parameters)
   super
end

The super method calls the original method (eg. test) in the
superclass and passes the same parameters as the originally invoked
method.

-John

--
John Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-15 00:59
(Received via mailing list)
>> Also, should I be overriding new or create or both?
overriding new should be enough since create calls new anyway.

      # File vendor/rails/activerecord/lib/active_record/base.rb,
line 443
443:       def create(attributes = nil)
444:         if attributes.is_a?(Array)
445:           attributes.collect { |attr| create(attr) }
446:         else
447:           attributes.reverse_merge!(scope(:create)) if scoped?
(:create)
448:
449:           object = new(attributes)
450:           object.save
451:           object
452:         end
453:       end


-John

--
John Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-15 01:09
(Received via mailing list)
Neither :)

Override initialize. Create calls new internally, and new calls
initialize.

class Person < ActiveRecord::Base
  alias :old_initialize :initialize
  def initialize(attributes = nil)
    old_initialize(attributes)
    # Do whatever you want in here.
  end
end

Note that your customised initialize method will be called for any
Person
object that is instantiated.

-Jonathan.
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-15 10:41
Jonathan Viney wrote:
> Neither :)
>
> Override initialize. Create calls new internally, and new calls
> initialize.

Thanks Jonathan. That's just what I needed.

Julian
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Felkins (Guest)
on 2006-06-11 20:10
(Received via mailing list)
Please help me understand this.

I would thing that you would be able to simply implement your own
initialize
method and perform a super inside, like this:

class Person < ActiveRecord::Base
  def initialize(*args)
    super(*args)
    # Do whatever you want in here.
  end
end

Yet this is not working for me.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Felkins (Guest)
on 2006-06-11 20:27
(Received via mailing list)
Nevermind. With additional searching I found an explanation at:

http://www.ruby-forum.com/topic/17456#11446

And this helpful blog entry:

http://blog.teksol.info/articles/2005/12/14/settin...
This topic is locked and can not be replied to.