# Constructor in model

**URL:** <https://www.ruby-forum.com/t/constructor-in-model/143221>\
**Category:** Rails\
**Created:** [July 16, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/constructor-in-model/143221 "2008-07-16T15:34:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [July 16, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/constructor-in-model/143221/1 "2008-07-16T15:34:13Z")

</div>

Hi all,

I have an activation model that when constructed should set the key  
(column in the activations table) to test.

class Activation \< ActiveRecord::Base  
belongs\_to :user

def initialize  
this.key = “test”  
end  
end

When i do ac = Activation.new I gget  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.include?  
from C:/rails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/  
active\_record/attribute\_methods.rb:202:in `method_missing' from C:/Documents and Settings/AIR14607/My Documents/prive/rails/ TeamGeist/app/models/activation.rb:5:in`initialize’  
from (irb):2:in `new’  
from (irb):2

Someone knows why?

thanks

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [July 16, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/constructor-in-model/143221/2 "2008-07-16T15:54:45Z")

</div>

On 16 Jul 2008, at 14:33, Tarscher wrote:

> this.key = “test”  
> TeamGeist/app/models/activation.rb:5:in `initialize' from (irb):2:in`new’  
> from (irb):2
> 
> Someone knows why?  
> You haven’t called the superclass initialize

Fred

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [July 16, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/constructor-in-model/143221/3 "2008-07-16T16:40:39Z")

</div>

On Jul 16, 2008, at 9:53 AM, Frederick C. wrote:

> > this.key = “test”  
> > TeamGeist/app/models/activation.rb:5:in `initialize' from (irb):2:in `new’  
> > from (irb):2
> > 
> > Someone knows why?  
> > You haven’t called the superclass initialize
> 
> Fred
> 
> > thanks

Is this something for which a before\_create callback would work?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [July 16, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/constructor-in-model/143221/4 "2008-07-16T17:24:53Z")

</div>

I always use this syntax:

def initialize(params = nil)  
super(nil)  
self.whatever ||= “blarg”  
end
