# ActiveRecord bottom half

**URL:** <https://www.ruby-forum.com/t/activerecord-bottom-half/108823>\
**Category:** Rails\
**Created:** [July 7, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823 "2007-07-07T17:57:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/1 "2007-07-07T17:57:10Z")

</div>

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](http://www.easesoftware.com) )

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

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/2 "2007-07-07T18:58:55Z")

</div>

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:

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=1)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

hth,  
bill

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/3 "2007-07-07T19:21:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/4 "2007-07-07T19:36:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/5 "2007-07-07T19:44:43Z")

</div>

Bill,

Please see:

[http://www.ruby-forum.com/topic/113189#new](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 ?

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/6 "2007-07-07T21:18:10Z")

</div>

Hi Perry,

Perry S. wrote:

> Please see:
> 
> [Validates\_presence\_of of polymophic fields - Rails - Ruby-Forum](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](http://api.rubyonrails.org) and seen the note re: the potential need to  
use  
validates\_presence\_of on the association itself.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 7, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/activerecord-bottom-half/108823/7 "2007-07-07T21:35:36Z")

</div>

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.
