Brian Candler
5/11/2007 4:42:00 PM
On Sat, May 12, 2007 at 01:19:26AM +0900, Brian Candler wrote:
> On Sat, May 12, 2007 at 12:30:00AM +0900, Bernd Burnt wrote:
> > This is the workaround I just had so far, and now I want to get rid of
> > it.
> >
> > I have two classes:
> > class Myclass < ActiveRecord::Base
> > def self.find(number)
> > Anotherclass.find(number)
> > end
> >
> > class Anotherclass < SAP4Rails::Base
> > def self.find(number)
> > do something
> > end
> >
> > But this solution to me seems to be so un-ruby!
>
> You seem to be doing something very strange in the above code. You are
> replacing one of ActiveRecord::Base's most fundamental operations - load
> from database by ID - with something which does something completely
> different.
>
> Are you also going to overwrite the save and update methods? In which case,
> what functionality from ActiveRecord::Base do you actually want to keep?
>
> I am guessing that SAP4Rails is some sort of remote-procedure call wrapper.
> That is, an instance of SAP4Rails::Base represents an object on some remote
> SAP server. If that's true, I don't see how it makes sense to try to mix
> that with another object which represents a row in a local SQL database.
>
> If Myclass represents something which may exist in the local database and/or
> in the remote SAP system, then I'd very much go for delegation.
>
> class MyDb < ActiveRecord::Base; end
> class MySap < SAP4Rails::Base; end
>
> class Myclass
> def self.find(number)
> @rec = MyDb.find(number)
> @sap = MySap.find(number)
> end
> end
>
> Then you are making it explicit that you are talking about two different
> things, and you can implement useful operations (such as copy data from one
> to the other)
P.S. Something which may be relevant as an example is ActiveResource.
This is a REST XML client library, which presents an API very similar to
ActiveRecord: e.g.
class Foo < ActiveResource::Base
self.site = "http://localhost:3000"
end
foo = Foo.find(2) # performs GET /foo/2.xml
foos = Foo.find(:all) # performs GET /foo.xml
But this doesn't re-use *any* of ActiveRecord. Whilst the interface is
similar in some ways, the underlying operations are very different.
ActiveRecord is very much a wrapper around a SQL database. Almost all the
operations you can perform ultimately get translated into calls on the
database connection for SELECT, UPDATE etc.
The other useful bits of ActiveRecord, like validations, are already mixins
in their own right. That is, they come pre-mixed into ActiveRecord::Base,
but you could mix them into your own class independently.
Regards,
Brian.