SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails style models?

    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

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Yakima WA.
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah and I just read about the method_missing method Got it

    http://www.rubycentral.com/ref/ref_c...method_missing


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •