Web Databases and Objects


#1

OK…I’m having a mental block.

I have a website started with Ruby on Rails.

I’ve got this object Hierarchy in mind.

the simple “it’s a database” stuff is fine.but when it comes to the
Arrays
of Hashes that make

up some of the objects within the custom class.the relationship to the
database and what happens when

I close down the environment (and in future want to start up site.)

So how do I Save and retrieve that stuff? do I have to do the
Object/Relational mapping myself or

do I depend on the “serialize” function/method for the class?

the examples I’ve seen don’t have a user create and populate a complex
object and then

save and retrieve it…things that are saved and retrieved seem pretty
simple—though there

are lots of instantiations and work live—but I need to create a
datastore
that gets extended and

hit with queries—Am I going to have to recreate and repopulate via
“deserialize” ?

I imagine the answer is obvious—but it eludes my feeble mind…

Help?

cj:-)


#2

On Jul 13, 2007, at 9:22 PM, Craig J. wrote:

the simple “it’s a database” stuff is fine.but when it comes to

save and retrieve it…things that are saved and retrieved seem pretty

I imagine the answer is obvious—but it eludes my feeble mind…

Help?

cj:-)

Rails is BIG. Take it slowly. I’m still struggling with it too.
There are some good books on it. Work through them. It takes time for
the details to start sinking in.
Don’t rush yourself. Rails comes with the stigma of fast development,
but the part about learning curve is often lost in the hype.
I’m frustrated with it myself, but the reality is, a framework takes
time to learn AND it pays off in the end because it’s better than
building all of that functionality yourself.
in the meantime, check out streamlined…

John J.


#3

John, appreciate your shot at it…

But—(and I don’t’ want to be a “Joe” here…) if anyone
has build a site/ruby app which creates and manipulates
complex object classes (things that have Arrays, or Hashes within
their Class definitions)…
How did you store those things in the database…

for example … if I have a recipe which has Steps
which have ingredients tied to those steps I might want to
create an array of Steps, each of which can have both Actions and
an array of ingredients…
But…once a user has put in the receipe…and I’ve populated all those
objects within the instance…
I want to save it…
How?

throw me some ideas…
I’m thinking that I have to serialize the class…and that is what’s
needed.

cj:)


#4

On Jul 13, 2007, at 10:04 PM, Craig J. wrote:

create an array of Steps, each of which can have both Actions and

So how do I Save and retrieve that stuff? do I have to do the

After creating your database schema, usually using script/generate
migration migration_name,

If you run the old
ruby script/generate scaffold
You’ll be faced with a simple CRUD application.
It includes the pretty standard Rails naming of methods in the
controllers, it’s in all of the tutorials online and elsewhere.
It is a bit cryptic as there isn’t an explicit ‘save by clicking
here’ or anything,
but basically, the controllers talk to active record and do SQL for you.
SQL doesn’t really do a ‘save’ as such, it saves as soon as the
record is created, the two are one and the same.
If you follow the conventions of Rails (I’m still learning them
myself, there is a lot to keep in your head)
naming conventions and what not, things should be saved and available
the next time.
The only thing to really shut down is the database and the server.
If the controllers, models, views and database are set up correctly,
your data should be saved.

I’m going to recommend the Sitepoint book Build Your Own Ruby On
Rails Applications, I just bought it today… based on a
recommendation or two that gave it high marks for going into details
like this. I’m also trying to get details like this, but different.
I’ll be reading it tomorrow and if I find something clearer, I’ll let
you know.

You could also try the Rails list but I’ve found it’s not too
beginner friendly sometimes. It’s pretty geared (like many of the
books) toward people that have built web apps in other languages before.


#5

Thanks.
This be useful…
I figured it was a “duh” moment coming…

Will try it out…
thanks again…


#6

On 7/13/07, Craig J. removed_email_address@domain.invalid wrote:

create an array of Steps, each of which can have both Actions and
an array of ingredients…
But…once a user has put in the receipe…and I’ve populated all those
objects within the instance…
I want to save it…
How?

throw me some ideas…
I’m thinking that I have to serialize the class…and that is what’s needed.

Do you have a relational model already designed?

Like, for example, with recipes, you might have

create table recipes (
id int,
name varchar,
primary key id
);

create table steps (
id int,
number int,
recipe_id int,
ingredient_id int,
primary key id
);

create table ingredients (
id int,
name varchar,
primary key id
);

Note, that’s just a hack-up. A real model would be more complex, and
would warrant constraints. But anyway, in Rails, these tables would
become classes that can become instantiated upon querying the
database.

Now, if you really just want to serialize data, then you wouldn’t
really need a relational database at all. You would, however, lose
the power of the database for queries and data integrity.

hth a little,
Todd