0
class Package
  include DataMapper::Resource
  property :id,           Serial
  property :product,      String, :required => true 

  def self.create(attributes = {})
    puts 'I am in the Object method'
    #do something here with value of product before creating a new row
    create(attributes)
  end
end


p = Package.new
p.create :product=>'myprod'

I actually want a wrapper around "create" method provided by Datamapper. So that before creating a row in Package table, I can do something with the value of "product". But above implementation is wrong and it seems it gets lost in circular calls. I get

.......
.......
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
SystemStackError - stack level too deep:

What am I doing wrong? and how can I achieve my goal

JVK
  • 3,782
  • 8
  • 43
  • 67

1 Answers1

4

What you have in your code is a recursive definition. You have to avoid that.

class Package
  include DataMapper::Resource
  property :id,           Serial
  property :product,      String, :required => true 

  def self.create(attributes = {})
    puts 'I am in the Object method'
    #do something here with value of product before creating a new row
    super(attributes)
  end
end
sawa
  • 165,429
  • 45
  • 277
  • 381
  • Thanks @sawa it works. if I use Package.create(......). But not if p = Package.new p.create :product=>'myprod' then it says undefined create method – JVK Oct 16 '12 at 20:20
  • If `create` is a class method, you can only use it as `Package.create`, which case this code is for. If it is an instance method, you use it in the form `Package.new.create`, in which case you need a different definition. You cannot do both. – sawa Oct 16 '12 at 23:20