How do I assign default values to model attribute

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.

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.

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.

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?

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?

use the

super

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

(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?