# How do I assign default values to model attribute

**URL:** <https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990>\
**Category:** Rails\
**Created:** [August 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990 "2007-08-25T16:45:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/1 "2007-08-25T16:45:18Z")

</div>

I have a model with a attribute named “code”. Every time i create an  
instance I want the “code” to be initialized by making a call to  
“generate\_code” method. How do i initialize model attributes in rails ??

thanks for help.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 25, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/2 "2007-08-25T21:48:58Z")

</div>

Jigar G. wrote:

> I have a model with a attribute named “code”. Every time i create an  
> instance I want the “code” to be initialized by making a call to  
> “generate\_code” method. How do i initialize model attributes in rails ??
> 
> thanks for help.

Add this line to your model class:  
before\_create :generate\_code

generate\_code will be called when you ‘save’ an new instance just before  
it is sent to the database. There are a number of these methods, called  
CallBacks available. You may what to look through a list of them and  
see if there isn’t one that might work better for your particular  
situation.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 26, 2007, 7:03am UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/3 "2007-08-26T07:03:37Z")

</div>

I want a call back on object construction, not on save or on create. but  
for now i think before\_create will work for me.

John M. wrote:

> Jigar G. wrote:
> 
> > I have a model with a attribute named “code”. Every time i create an  
> > instance I want the “code” to be initialized by making a call to  
> > “generate\_code” method. How do i initialize model attributes in rails ??
> > 
> > thanks for help.
> 
> Add this line to your model class:  
> before\_create :generate\_code
> 
> generate\_code will be called when you ‘save’ an new instance just before  
> it is sent to the database. There are a number of these methods, called  
> CallBacks available. You may what to look through a list of them and  
> see if there isn’t one that might work better for your particular  
> situation.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 26, 2007, 9:38am UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/4 "2007-08-26T09:38:23Z")

</div>

Sounds good, I try it. I hope it dosen’t conflict with the default  
constructor of AR class.

Shai R. wrote:

> class Klass
> 
> def initialize(default\_att1, att2, att3)  
> @default\_att = default\_att1  
> @att2 = att2  
> end
> 
> end
> 
> whenever you pass a .new method, initialize is called:
> 
> var = Klass.new(10, ‘’, ‘value’)
> 
> var is now
> 
> var.default\_att1 = 10  
> var.att2 = ‘’  
> var.att3 = ‘value’
> 
> helps?

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 26, 2007, 9:34am UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/5 "2007-08-26T09:34:55Z")

</div>

Jigar G. wrote:

> I want a call back on object construction, not on save or on create. but  
> for now i think before\_create will work for me.
> 
> John M. wrote:
> 
> > Jigar G. wrote:
> > 
> > > I have a model with a attribute named “code”. Every time i create an  
> > > instance I want the “code” to be initialized by making a call to  
> > > “generate\_code” method. How do i initialize model attributes in rails ??
> > > 
> > > thanks for help.
> > 
> > Add this line to your model class:  
> > before\_create :generate\_code
> > 
> > generate\_code will be called when you ‘save’ an new instance just before  
> > it is sent to the database. There are a number of these methods, called  
> > CallBacks available. You may what to look through a list of them and  
> > see if there isn’t one that might work better for your particular  
> > situation.

how about using

class Klass

def initialize(default\_att1, att2, att3)  
@default\_att = default\_att1  
@att2 = att2  
end

end

whenever you pass a .new method, initialize is called:

var = Klass.new(10, ‘’, ‘value’)

var is now

var.default\_att1 = 10  
var.att2 = ‘’  
var.att3 = ‘value’

helps?

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [August 26, 2007, 11:23am UTC](https://www.ruby-forum.com/t/how-do-i-assign-default-values-to-model-attribute/112990/6 "2007-08-26T11:23:07Z")

</div>

use the

super

special keyword; it inherits the parent’s class methods, so it takes the  
super method, and modifies it for your needs:

> **[Rails and the initialize() method](http://jamescrisp.blogspot.com/2006/09/rails-and-initialize-method.html)**
>
> I spent a bit of time this weekend on a pet rails project. I came across a strange error when trying to create new records through the appli...

(in the link-example, you don’t need the “if” clause supplied there)

Jigar G. wrote:

> Sounds good, I try it. I hope it dosen’t conflict with the default  
> constructor of AR class.
> 
> Shai R. wrote:
> 
> > class Klass
> > 
> > def initialize(default\_att1, att2, att3)  
> > @default\_att = default\_att1  
> > @att2 = att2  
> > end
> > 
> > end
> > 
> > whenever you pass a .new method, initialize is called:
> > 
> > var = Klass.new(10, ‘’, ‘value’)
> > 
> > var is now
> > 
> > var.default\_att1 = 10  
> > var.att2 = ‘’  
> > var.att3 = ‘value’
> > 
> > helps?
