ActiveRecord bottom half

I want a different way of doing polymorphic records. One problem I have
is validates_presence_of does not work for me – and no one replied to
my question. I am finding it hard to make sure the database stays
consistent.

What I want to do is create a table that has a unique id and a string
which will be the type. The other tables that type will point to will
have the same id and the attributes for that type. So, the first table
may be items and items could reference people, companies, etc. The key
to people, companies, etc is the same id as for item – i.e. if you
union all of the sub tables, you will have each id at most one time.

I believe, using PostgreSQL’s features, this will be easier to keep
consistent. Of course, I may find out that I’m full of BS.

But, part of the knowledge I need to gain is how ActiveRecord goes from
the blob of mess that the database returns to an instance of a
particular class. If anyone can help me out and tell me where to look,
etc, that would be much appreciated.

Thank you,
Perry S.
Ease Software, Inc. ( http://www.easesoftware.com )

Low cost SATA Disk Systems for IBMs p5, pSeries, and RS/6000 AIX systems

Hi Perry,

Perry S. wrote:

What I want to do is create a table that has a unique id
and a string which will be the type. The other tables that
type will point to will have the same id and the attributes
for that type. So, the first table may be items and items
could reference people, companies, etc. The key
to people, companies, etc is the same id as for item – i.e. if you
union all of the sub tables, you will have each id at most one time.

Your description above contains much that we allow Rails and AR to
manage
for us. One of the key things with Rails is that primary keys are
meaningless to us. Only AR cares about them, and we let it take control
of
them to make it easier for us to focus on the application itself rather
than
the management of the data the app uses. You’ll find it a lot easier to
work with Rails, and to get help here too IME, if you can start by
answering: “From a user perspective, what problem are you solving? What
is
it you’re trying to allow the user to do with your application?”

I believe, using PostgreSQL’s features, this will be easier
to keep consistent. Of course, I may find out that I’m full
of BS.

I haven’t used PostgreSQL with Rails, but don’t have the impression from
seeing OP’s posts that it’s any easier or harder than any other
supported DB
with Rails.

But, part of the knowledge I need to gain is how ActiveRecord
goes from the blob of mess that the database returns to an instance
of a particular class.

Depending on exactly what your objective is, you may or may not need to
know
the above in order to achieve your objective. Probably not. In
general,
our apps deal in objects, we tell AR how the objects are related, it
handles
the ORM mapping, and we’re happy for it.

If anyone can help me out and tell me where to look,
etc, that would be much appreciated.

I’d start out by trying to regain my “beginner eyes.” Will R. said
“It’s not what we don’t know that hurts. It’s what we know that ain’t
true.”
To be successful with Rails, you’re probably going to do quite a bit of
‘unlearning.’ That’s just speculation on my part, based on a year and a
half of observation / participation here on the list.

There are a number of beginner tutorials out there. With all things
Rails,
Google is your friend. You may find it too elementary for you, but you
might want to start with:
http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html?page=1

hth,
bill

Bill W. wrote:

Hi Perry,

Perry S. wrote:

What I want to do is create a table that has a unique id
and a string which will be the type. The other tables that
[ snip ]
hth,
bill

The big thing that is frustrating me with the existing methods is how to
do validation. As I said, I can not get validates_presence_of to work
for me in a polymorphic belongs_to. I’m torn between trying to do
validation up in Rails or down in the DB. As I said, this may be all
folly but I’ve implemented the application (the basic functions) using
the standard Rails concepts. But, I can not figure out how to make sure
the thing the belongs to association really does exist and I can not
figure out a way to prevent it from being deleted before everything that
points to it is deleted first. Those two things is what led me down
this journey.

What I am trying to do is just subtly different for sure but the
rearrangement of the tables and keys will make the DB validation part
much easier. It also reduces the amount of redundant information.

The only thing that PostgreSQL gives me is rules and stored procedures
to do the validation in the DB. I’m not sure but I think those are not
in other DB’s.

To recap, I’m not wanting to go down this path. I just want to validate
the database and keep it valid at all times.

Hi Perry,

Perry S. wrote:

The big thing that is frustrating me with the existing
methods is how to do validation. As I said, I can not
get validates_presence_of to work for me in a
polymorphic belongs_to.

It would be easier to help if you posted the SQL script or migration
that
you’re using to create the tables and the related models.

I’m torn between trying to do validation up in Rails
or down in the DB.

We do validations in our models. Rails treats the DB as a simple
container.
My experience is that that’s the only viable option if you want the
freedom
to change databases.

The only thing that PostgreSQL gives me is rules and stored
procedures to do the validation in the DB. I’m not sure but I
think those are not in other DB’s.

They’re not in some, and they’re handled differently in others. It’s
one of
the fundamental ways that database vendors achieve lockin. Being locked
in
to an open source DB is still being locked in. Not good, IMHO.

To recap, I’m not wanting to go down this path. I just
want to validate the database and keep it valid at all times.

That’s not going to be a problem, assuming you’re willing to let Rails
and
AR do their thing, rather than impose a preconceived solution on them.

Post some code.

Best regards,
Bill

Bill,

Please see:

http://www.ruby-forum.com/topic/113189#new

I may have discovered the error of my ways. Instead of
validates_presence_of, should I be using validates_associated ?

Hi Perry,

Perry S. wrote:

Please see:

http://www.ruby-forum.com/topic/113189#new

I may have discovered the error of my ways. Instead of
validates_presence_of, should I be using validates_associated ?

What you don’t say in your post above is whether or not you’re getting
the
results you expect. If it accomplishes what you’re trying to
accomplish,
use it. Rails typically gives us more than one way to “skin the cat.”
Without knowing what you’re trying to accomplish it’s not possible to
say
whether or not there’s an easier / more Railsy way to do it. I assume
you’ve reviewed the documentation on validates_associated at
http://api.rubyonrails.org and seen the note re: the potential need to
use
validates_presence_of on the association itself.

Best regards,
Bill

Bill W. wrote:

Hi Perry,

What you don’t say in your post above is whether or not you’re getting
the
results you expect.

I thought I did. The validates_presence_of always produces an error.

I’m trying to make sure that the child and parent have been set and are
not null.

At the time validate gets called, child_id and child_type are not set
(according to my logging). But they get set before they actually get
saved.

The relationship is being saved because I have appended it to
@person.children. i.e.

@person.children << relationship (as you can see in my other post).

So, somehow, before the data actually is shipped off to the database but
after the validation sequence, the child_id and child_type get filled
in.

I’m still trying to debug this and find a way to get this to work. I
thought I had it figured out but the last test didn’t work.

Oh, also, if I do both:

@person.children << relationship
relationship.child = @person

that causes an infinite recursion.

This seems pretty basic. I must be doing something really silly but I
don’t know what it is.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs