# Alias of Table?

**URL:** <https://www.ruby-forum.com/t/alias-of-table/84349>\
**Category:** Rails\
**Created:** [February 3, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/alias-of-table/84349 "2007-02-03T15:22:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [February 3, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/alias-of-table/84349/1 "2007-02-03T15:22:38Z")

</div>

I’m attempting to convert a legacy application.

I need to do what amounts to a “alias filter” on a table.  
The table - call it ‘Assets’ - has many name-value pairs as its public  
data.  
It is shared by many other tables, so the key is

```
 parent_id, parent_class

```

This is a one-way association: “Assets of X”, where “X” can be another  
table.

In base SQL this is easy - set up an alias

My Question- ow to do this in Rails:  
Is there a way to filter for a ‘parent’ class so this can be shared?

Class OneThing \< AR::B  
has\_many :assets # where asset.parent\_class = ‘OneThing’

Class AnotherThing \< AR::B  
has\_many :assets # where asset.parent\_class = ‘AnotherThing’

So how to make this ‘automated’ for the CRUD and how to define the  
‘Setting’  
class? Can ‘Asset’ somehow belong\_to more than one model?

I’ve no problem drawing the E-R diagram 🙂  
or thinking what the explicit and long winded SQL would be,  
but I don’t have the Rail experience to see this.

I can see docco on ‘has\_and\_belongs\_to\_many’ using an intermediate join  
table, but this is a one way association.

## Thanks /a

A seminar on time-travel will be held two weeks ago.

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [February 4, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/alias-of-table/84349/2 "2007-02-04T18:06:45Z")

</div>

Anton A. wrote:

> I’m attempting to convert a legacy application.
> 
> I need to do what amounts to a “alias filter” on a table.  
> The table - call it ‘Assets’ - has many name-value pairs as its public  
> data.  
> It is shared by many other tables, so the key is
> 
> ```
> parent_id, parent_class
> 
> ```
> 
> This is a one-way association: “Assets of X”, where “X” can be another  
> table.
> 
> In base SQL this is easy - set up an alias
> 
> My Question- ow to do this in Rails:  
> Is there a way to filter for a ‘parent’ class so this can be shared?
> 
> Class OneThing \< AR::B  
> has\_many :assets # where asset.parent\_class = ‘OneThing’
> 
> Class AnotherThing \< AR::B  
> has\_many :assets # where asset.parent\_class = ‘AnotherThing’
> 
> So how to make this ‘automated’ for the CRUD and how to define the  
> ‘Setting’  
> class? Can ‘Asset’ somehow belong\_to more than one model?
> 
> I’ve no problem drawing the E-R diagram 🙂  
> or thinking what the explicit and long winded SQL would be,  
> but I don’t have the Rail experience to see this.

Rails does this using something called a polymorphic association. It’s  
the only explicit support in Rails for composite foreign keys. Your  
foreign key is composed of the values parent\_id and parent\_type. The  
class name of the parent model is stored as a string in parent\_type.

You set this up using the :polymorphic =\> true option on the :parent  
association

belongs\_to :parent, :polymorphic =\> true

Then in the various classes, use :as =\> :parent on the has\_many

has\_many :assets, :as =\> :parent

There are a few limitations related to this being a one-way association.  
You can’t do eager loading of parents, and has\_many :through needs some  
help to be useful. Otherwise it works pretty well.

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [February 5, 2007, 12:45am UTC](https://www.ruby-forum.com/t/alias-of-table/84349/3 "2007-02-05T00:45:32Z")

</div>

Josh S. said the following on 02/04/2007 12:06 PM:

> Then in the various classes, use :as =\> :parent on the has\_many
> 
> has\_many :assets, :as =\> :parent

That seems odd to me, but what do I know. If I knew I wouldn’t be asking  
🙂

So this would work:

class Asset \< AR:B  
belongs\_to :parent, :polymorphic =\> true

def set(name, value)  
end

def value\_of(name)  
end  
end

class Person \< AR:B  
has\_many :assets, :as =\> :parent  
end

class Corporation \< AR:B  
has\_many :assets, :as =\> :parent  
end

class Bank \< Corporation  
has\_many :assets, :as =\> :parent  
end

class Government \< AR:B  
has\_many :assets, :as =\> :parent  
end

class CIA \< AR:B  
has\_many :assets, :as =\> :parent  
end

class Harry \<AR:B  
has\_many :assets, :as =\> :parent

def make\_my\_asset(date, punkname)  
assetname = date.to\_string  
…  
end  
end

> There are a few limitations related to this being a one-way association.

That’s OK, I suppose, so long as you want to know the assets belonging  
to  
something and not who owns a particular asset.

> You can’t do eager loading of parents, and has\_many :through needs some  
> help to be useful. Otherwise it works pretty well.

Help? useful?  
By ‘help’ do you mean if one of the parents needed to create or delete  
an  
asset? Or does Rails ‘magically’ fill in the parent\_id and parent\_class  
somehow.

Would it help if these parents were a subclass of an AssetOwner class?

–  
Authors (and perhaps columnists) eventually rise to the top of whatever  
depths they were once able to plumb.  
– Stanley Kaufman
