The answer is easy - @var and self.var are not the same. All
variables in Ruby (it''s a Ruby question, not Rails) are private and
you can never make them public directly. What is usually done is
creating getters and seters with the same name. Like this
class Foo
def var
@var
end
def var=(v)
@var = v
end
end
or like this (which does he same for you):
class Foo
attr_acessor :var
end
So when you are talking about ActiveRecord objects, you must use
self.var, to call the magic methods created for you by ActiveRecord.
Cheers,
Yuri
On 5/20/07, itsnotvalid
<itsnotvalid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I started learning rails by following the agile way. It comes to the
> point where it puzzles me.
>
> class Puzzle < ActiveRecord::Base
>
> # virtual var, not existing in the database
> def var
> @var
> end
>
> def var=(foo)
> @var= foo
> @var_in_database = @var + 1 # var_in_database exists in database
> end
>
> end
>
> Then when I do a save of this model it does nothing. I know that if I
> instead put self.var_in_database it would work perfectly, but from my
> limited knowledge of ruby @var should be working like self.var in most
> cases. Now it is the rare case but I can''t find the reason why
this
> doesn''t work.
>
> Please solve my puzzle, thank you.
>
>
> >
>
--
Best regards,
Yuri Leikind
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---