# Autosetting field before validation

**URL:** <https://www.ruby-forum.com/t/autosetting-field-before-validation/123862>\
**Category:** Ruby\
**Created:** [December 21, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862 "2007-12-21T14:35:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [December 21, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862/1 "2007-12-21T14:35:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [December 21, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862/2 "2007-12-21T17:11:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [December 21, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862/3 "2007-12-21T18:53:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [December 21, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862/4 "2007-12-21T19:50:45Z")

</div>

On Dec 21, 6:52 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [December 21, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/autosetting-field-before-validation/123862/5 "2007-12-21T19:52:30Z")

</div>

> 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
