Chet Hendrickson and I are just getting back to Ruby after
too long away, and we''ve just started working through the Rails
book. Mostly happy so far, though we''re just a couple of days in.
One thing that confused me was the validate method in the first
example. I''m sure I''m missing something really obvious, but
I''d
rather be embarrassed than confused. ;-> The validate is:
def validate
errors.add(:price, "should be positive") unless price.nil? ||
price >= 0
end
I don''t see how "price" can refer to the field in the record,
unless
price is perhaps an accessor kind of method. But in the class
definition, I don''t see any declaration of a price method.
How does that access to price work?
Thanks,
Ron Jeffries
www.XProgramming.com
If there''s only one answer, then this must not be a very interesting
topic.
On Sep 14, 2005, at 8:34 PM, Ron Jeffries wrote:> Chet Hendrickson and I are just getting back to Ruby after > too long away, and we''ve just started working through the Rails > book. Mostly happy so far, though we''re just a couple of days in. > > One thing that confused me was the validate method in the first > example. I''m sure I''m missing something really obvious, but I''d > rather be embarrassed than confused. ;-> The validate is: > > def validate > errors.add(:price, "should be positive") unless price.nil? || > price >= 0 > end > > I don''t see how "price" can refer to the field in the record, unless > price is perhaps an accessor kind of method. But in the class > definition, I don''t see any declaration of a price method. > > How does that access to price work? >A good question. Though I''m not looking at the page itself, I''m betting that this is where ActiveRecord does its magic--the ''price'' accessor method is created dynamically when the class itself is defined (ActiveRecord knows how to load the table definitions directly from the database, thus it can know what to name the corresponding accessor methods). Hope that helps, Duane Johnson (canadaduane)
Ron Jeffries <ronjeffries-HInyCGIudOg@public.gmane.org> writes:> I don''t see how "price" can refer to the field in the record, unless > price is perhaps an accessor kind of method. But in the class > definition, I don''t see any declaration of a price method. > > How does that access to price work?ActiveRecord "automatically" knows about database fields by using MethodMissing to reflect on the db scheme. So, yes, price is an accessor method for the database field. Since price isn''t defined anywhere, ActiveRecord assumes it''s a database field. If it''s not, then it finally complains about MethodMissing. -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On Sep 14, 2005, at 10:34 PM, Ron Jeffries wrote:> def validate > errors.add(:price, "should be positive") unless price.nil? || > price >= 0 > end > > I don''t see how "price" can refer to the field in the record, unless > price is perhaps an accessor kind of method. But in the class > definition, I don''t see any declaration of a price method. > > How does that access to price work?Hey Ron, I assume you have a price column in your table and that this class is an ActiveRecord::Base subclass. Under that assumption, your price is an accessor, generated for you because you subclassed ActiveRecord::Base. The beauty of Ruby, Rails and DRY. Alas, I only follow the list. I have only had one, short affair with Rails. So, if I''m wrong, forgive me. Mostly wanted to say hello. Adam Williams (RoleModel)
> > Ah ... /dynamically/. It definitely could do that, of course. Maybe > that''s written up somewhere that I missed. Good call, you''re surely > right. > > On Wednesday, September 14, 2005, at 10:59:03 PM, Doug Alcorn wrote: > > >> ActiveRecord "automatically" knows about database fields by using >> MethodMissing to reflect on the db scheme. So, yes, price is an >> accessor method for the database field. Since price isn''t defined >> anywhere, ActiveRecord assumes it''s a database field. If it''s not, >> then it finally complains about MethodMissing. >> > > So ... do we know whether it''s doing that by fielding the exception, > or by predefining the method? I''m just curious ... always used to > like to catch #doesNotUnderstand: in Smalltalk and do odd things > with it. >I think it''s the latter. If you call, for example, "User.respond_to? :first_name" it will return false, even though first_name is an accessor. This behavior is typical of a MethodMissing catch... just like #doesNotUnderstand I''m guessing!> Thanks, gents! > > Ron Jeffries > www.XProgramming.com > Now -- Bring me that horizon. -- Captain Jack Sparrow > >