From: Hal Fulton Date: 2005-10-30T03:19:55+09:00 Subject: Re: The "perfect" ORM? Kirk Haines wrote: > > In some cases, you can't do without it. > > ORM can be approached from two basic directions. It can be database driven, > in that the structure of the database dictates the language structures, or > one can have the language structures dictate the database structure. I think I am definitely object-driven. [snippage] > And the reason why relationships are useful beyond having a simple mapping of > class to table is because they make the code convenient. > > In the MSDS application that my examples come from, one can select a single > chemical and view information on it. One thing that can be done at that > point is to see other chemicals manufactured by the same manufacturer that > the chemical being viewed comes from. > > So, if that chemical's record is in @chemical: > > @chemical.manufacturer.chemicals > > And you have your list. The relationship information provided the necessary > string to tie those together without the programmer having to explicity write > the code. It is a tremendous timesaver. It is, and I see the usefulness of it, but it doesn't fit my brain. That is the sort of thing I'd use a query for, rather than just grabbing the value of what looks to me like an accessor. When you call chemicals in that way, is it then doing a query (late binding) or did it get done recursively when you retrieved @chemical? > Now, the other direction that an ORM can go is to let the language structures > dictate the database structures. > > So, for instance, you declare a class: > > class Foo > attr_accessor :a, :b > end > > And somehow, that class is mapped to the database, creating or altering the > table definition as necessary. When doing this, if a field is going to store > objects of another class that is also mapped to a table in the database, you > have to tell the ORM about that so that it knows how the tables should look > in order to store your data. [snip] Yes, this is my personal preference. [snip] > What if you have a Manufacturers object, and you want to know all of the > chemicals that have that manufacturer? You could write a method manually to > do that: > > class Manufacturers > def chemicals > #query the db and retrieve an array of Chemicals records where > # manufacturer == self.idx > end > end > > But if all of that typing can be reduced to simply telling the ORM about the > relationship, using some syntax or other, isn't that a win? > > class Manufacturers > relates_to Chemicals.manufacturer > end It's a win if you want to think that way. I'd rather just make the query syntax easy/flexible and forget about "relates_to" and such. I understand a simple query. But every time I saw "relates_to" I would have to stop and ask myself how it worked and what it meant. Hal