[Rails] Re: why do I have to refer to my attr_accessible as self.varname?
On Dec 21, 6:12 pm, Dan Brooking <dmbrook...@gmail.com> wrote:
> class Page < ActiveRecord::Base
> attr_accessible :url, :title
>
> after_initialize :parse_page_params
>
> def parse_page_params
> @title = "test"
> end
>
> and this wasn't working... I understand what you said above about the
> instance variables, methods, initializing, etc.. but still a little unclear
> about why that code doesn't work as I'm setting it. Is it because Rails
> uses the method name of title which hasn't been initailized in my
> assignment above?
>
Because rails doesn't use individual instance variables to store your
attributes (whether they've been marked as attr_accessible makes no
difference)
title = "foo"
Doesn't work because ruby assumes that you want to assign to the local
variable called title. Doing self.title = makes it clear that you want
to call the title= accessor
Fred
> On Fri, Dec 21, 2012 at 12:43 PM, Walter Lee Davis <wa...@wdstudio.com>wrote:
>
>
>
>
>
>
>
>
>
> > On Dec 21, 2012, at 12:37 PM, Dan Brooking wrote:
>
> > > I posted a previous message about overriding initialize... because I was
> > having issues setting some of the parameters. I have a Page model that has:
>
> > > attr_accessible :url, :title, :doc, :domain
>
> > > and it's called via:
>
> > > Page.new(:url => 'http://www.yahoo.com')
>
> > > Since I'm only passing in the url to new, I needed to set the other
> > parameters. I was trying to do this via an after_initialize callback which
> > wasn't working so tried overriding initialize... still not working.
>
> > > What I found out was that in my after_initialize, I was referring to
> > title as @title which is why it was not working. I switched it to
> > self.title and it works fine.
>
> > > My question is - why?
>
> > @title is an instance variable. Until you set it, it doesn't exist. Having
> > a method on the model called title (or an accessor, or some other Rails
> > magick) does not instantiate that method's return until and unless you ask
> > for it by calling the method. Calling self.method_name just makes it clear
> > which same-named method you really mean. Self is implied much of the time,
> > but when you have all the many method_missing options available, it might
> > not be the first one such that gets called.
>
> > Walter
>
> > > --
> > > 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@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscribe@googlegroups.com.
> > > To view this discussion on the web visit
> >https://groups.google.com/d/msg/rubyonrails-talk/-/-ncNakybQ-cJ.
> > > For more options, visithttps://groups.google.com/groups/opt_out.
>
> > --
> > 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@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscribe@googlegroups.com.
> > For more options, visithttps://groups.google.com/groups/opt_out.
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home