Forum: Ruby Activerecord associations like class structure

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3518ff338e5eee9b32eb1815daabc602?d=identicon&s=25 Marcelo Barbudas (Guest)
on 2009-05-07 14:47
(Received via mailing list)
Hi,

This question is based on another thread that helped me realize the
initial idea I had about a library format was not good.

How is it possible to mimic an ActiveRecord association like class
structure?

Something like:
Module Something
  Class Main
    def connection
    end
  end

  Class Resource < Main
    def  method
       call_resource
    end
  end
end

and being able to create something like
con = Main.new(user, pass)
result = con.resources.method

Thanks.

Sorry for opening another thread about this.
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-05-08 01:19
(Received via mailing list)
On Thu, May 7, 2009 at 5:46 AM, Marcelo Barbudas <nostef@gmail.com>
wrote:
>  Class Main
>
> and being able to create something like
> con = Main.new(user, pass)
> result = con.resources.method
>
> Thanks.
>
> Sorry for opening another thread about this.
>

One way (and one that lets you either use the default connection on
the base class or override it per sub-class) would be something like:

module Generic
  class Base
    def self.connect(resource, opts)
      @connection = resource.connect(opts)
    end

    def connection
      @connection || (self!=Generic::Base && Generic::Base.connection)
    end
  end
end

class Resource < Generic::Base
# whatever
end
This topic is locked and can not be replied to.