I understand I can just define a class without extending
ActiveReord::Base, but what if I want to use the model object
sometimes without having the data persist. For example…
Let’s say I have an “AutoManufacturer” object it might have
name=Mercedes,etc. But AutoManufacturer could contain a collection of
“Automobile” objects, (class=CLS, model=“CLS350, numberOfDoors=4”).
Maybe your db isn’t the one storing ALL the Automobile objects, you
might only want to store ‘favorite automobiles’ but you still want to
collect all the Automobile information (class, model, etc) when you
record a favorite.
So now what if I go to my DB and get my “AutoManufacturer” objects and
for each AutoManufacturer I query some webservice to return a list of
Automobiles and I want to then display on a page the AutoManufacturers
and a collection of the Automobile objects I returned from my
webservice call. If I have “Automobile” defined as an ActiveRecord
type when I do something like…
automobiles = getCarsFromWebservice()
automobiles.each do |a|
car = Automobile.new();
car.class= a.class
car.type= a.type
autoManufacturer.autos << car
end
every new car created above and added to the autoManufacturer actually
creates a new car in the db, In this case all I want is fully
populated list of autos in each autoManufacturer object for use to
display on the front end, but I don’t really want the automobile added
to the db at this time. (Maybe after they select some, I’ll then query
a webservice and populate a real Automobile that I do want to persist
in my db.)
Currently I’m having to do something really lame. I’m creating
basically identical objects - one a model object and one a simple
pojo. It doesn’t seem very dry to do it this way though. What’s a
better approach?
–
Rick