Hi,
I was just wondering how rails makes the attributes of a model part of the model class like:
User.username =
How do you get that username= method dynamically generated?
Thanks
| SitePoint Sponsor |



Hi,
I was just wondering how rails makes the attributes of a model part of the model class like:
User.username =
How do you get that username= method dynamically generated?
Thanks
Use the source Luke.
http://dev.rubyonrails.org/browser/t...record/base.rb
![]()

It uses a method_missing hack to dynamically generate the getters and setters for your db tables. You can get similar behavior from your pure ruby classes using the following technique:
Code:class Foo attr_accessor :bar attr_accessor :qux end a = Foo.new a.bar = "test" a.qux = "another test" puts a.bar puts a.qux



Ah and I just read about the method_missing methodGot it
http://www.rubycentral.com/ref/ref_c...method_missing
Bookmarks