[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[Rails 2.0] Autosetting field before validation

Maciej Piechotka

12/21/2007 1:21:00 PM

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've probably left my head... somewhere. Please wait untill I find it.
Homepage (pl_PL): http://uzytkownik....
(GNU/)Linux User: #425935 (see http://count...)

4 Answers

Andrew Stewart

12/21/2007 4:11:00 PM

0


On 21 Dec 2007, at 13:34, Maciej Piechotka 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 Stewart

-------
http://airbladeso...




James Britt

12/21/2007 5:52:00 PM

0

Maciej Piechotka wrote:
> Why this code do not work(permalize is from plug-in):

Rails questions are better suited for the Rails mailing list.

--
James Britt

"The greatest obstacle to discovery is not ignorance, but the illusion
of knowledge."
- D. Boorstin

Maciej Piechotka

12/21/2007 6:47:00 PM

0

> 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 Stewart
>

Regards

Maciej Piechotka

12/21/2007 6:47:00 PM

0

On Dec 21, 6:52 pm, James Britt <james.br...@gmail.com> wrote:
> Maciej Piechotka 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