Autosetting field before validation

Why this code do not work(permalize is from plug-in):

def before_validation_on_create
permalink ||= name.permalize unless name.nil?
end

Regards

PS.
The method is called but it do not change attribute. How to change it?

On 21 Dec 2007, at 13:34, Maciej P. wrote:

Why this code do not work(permalize is from plug-in):

def before_validation_on_create
permalink ||= name.permalize unless name.nil?
end

Regards

PS.
The method is called but it do not change attribute. How to change it?

I think permalink is treated as a local variable, i.e. local to your
method’s scope. Try self.permalink:

def before_validation_on_create
self.permalink ||= name.permalize unless name.nil?
end

Also have you checked that name isn’t nil?

Regards,
Andy S.

Maciej P. wrote:

Why this code do not work(permalize is from plug-in):

Rails questions are better suited for the Rails mailing list.


James B.

“The greatest obstacle to discovery is not ignorance, but the illusion
of knowledge.”

  • D. Boorstin

On Dec 21, 6:52 pm, James B. [email protected] wrote:

Maciej P. wrote:

Why this code do not work(permalize is from plug-in):

Rails questions are better suited for the Rails mailing list.

OK. I will.

Regards

Try self.permalink:

def before_validation_on_create
self.permalink ||= name.permalize unless name.nil?
end

It works.

Also have you checked that name isn’t nil?

Yes of course - many times via ./script/console.

Regards,
Andy S.

Regards