From: Devin Mullins Date: 2005-09-15T11:52:07+09:00 Subject: Re: params v.s. @params in rails? Ron Jeffries wrote: >David, 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" canrefer to the field in the record, unless price >isperhaps an accessor kind of method. But in the class definition, I don't see >any declaration of a price method. > >If it's not too much trouble, since we seem to be close to the topic ... what's >up with that? How does that access to price work? > > Ron, I'm nowhere near a Rails expert, and I'm definitely not David, but I do own the book, and the example you're referring to (page 62) says that this code goes in the model object. In the model object, yes, price is a method invocation on self (a Product object) that returns the content of the price column for that row. In Ruby, any undecorated symbolic reference 'whatever' looks for a local variable first, and if not defined? whatever, calls self.whatever. If you're pulling up script/console and doing a Product.instance_methods and not finding "price" listed, don't fret. It's most likely using Product#method_missing (i.e. #doesNotUnderstand). Hope that answers your question. Also, hope it's right. :) Devin