Activerecord associations like class structure

  • Thread starter Marcelo Barbudas
  • Start date
M

Marcelo Barbudas

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.
 
C

Christopher Dicely

Hi,

This question is based on another thread that helped me realize the initi= al
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
=C2=A0Class Main
=C2=A0 def connection
=C2=A0 end
=C2=A0end

=C2=A0Class Resource < Main
=C2=A0 def =C2=A0method
=C2=A0 =C2=A0 =C2=A0call_resource
=C2=A0 end
=C2=A0end
end

and being able to create something like
con =3D Main.new(user, pass)
result =3D 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 =3D resource.connect(opts)
end

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

class Resource < Generic::Base
# whatever
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,406
Latest member
ElizabetMo

Latest Threads

Top