On Friday, 21 December 2012 13:12:35 UTC-5, Dan Brooking wrote: > > So is the way I'm doing it right? Or just a way I happened to hack it to > work? > > The way my code was looking was basically: > > Page.new(:url => 'http://www.yahoo.com') > > 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? > > It sounds like you've got attr_accessible and attr_accessor somewhat entangled in your mental model. They aren't really the same thing at all:
- attr_accessible: specifies which attributes are mass-assignable (through things like Page.new(:some_attribute => 'foo')) and which have to be assigned individually. - attr_accessor: creates two accessor methods that wrap an instance variable. So this: attr_accessor :something is shorthand for this: def something @something end def something=(v) @something = v end Occasionally you'll even see both used for a particular name, as the developer wants to create an attribute that isn't persisted to the database but can be mass-assigned (from a form submission, for instance). --Matt Jones -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/idE5s9Ki5-cJ. For more options, visit https://groups.google.com/groups/opt_out.