# Help active record modeling

**URL:** <https://www.ruby-forum.com/t/help-active-record-modeling/166716>\
**Category:** Rails\
**Created:** [April 28, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/help-active-record-modeling/166716 "2009-04-28T21:49:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 28, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/help-active-record-modeling/166716/1 "2009-04-28T21:49:10Z")

</div>

Hi.

I have difficulties to model active record.  
So please help my trouble.

Let’s suppose there are nations which have castles at least 1,  
and there are 3 types of castles.

So I will make these tables.  
NationsTable,  
CastlesTable have common attributes of 3 types of castle.  
CastlesATable, CastlesBTable, CastlesCTable each have its unique  
attributes.

In this case how can I represent this DB to AR?  
I think  
class Nation \< ActiveRecord::Base  
has\_many :castles  
end  
class Castle \< ActiveRecord::Base  
belongs\_to :Nations  
end

but I’m not sure of what will be coded to these models.

class CastleA \< ActiveRecord::Base  
end  
class CastleB \< ActiveRecord::Base  
end  
class CastleB \< ActiveRecord::Base  
end

please help me.

and second question is  
when nation deleted, CastleA, CastleB have to be deleted but not for  
the castleC type.  
how can I do?

thanks for reading.

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 29, 2009, 2:32am UTC](https://www.ruby-forum.com/t/help-active-record-modeling/166716/2 "2009-04-29T02:32:25Z")

</div>

2009/4/29 serenobs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> NationsTable,  
> CastlesTable have common attributes of 3 types of castle.  
> CastlesATable, CastlesBTable, CastlesCTable each have its unique  
> attributes.

Easier to call your tables: ‘nations’ for ‘Nation’ model and ‘castles’  
for  
‘Castle’ model … etc

> In this case how can I represent this DB to AR?  
> I think  
> class Nation \< ActiveRecord::Base  
> has\_many :castles  
> end  
> class Castle \< ActiveRecord::Base  
> belongs\_to :Nations  
> end

Using the naming convention I suggested above you’d say:  
belongs\_to :nation

After all, a castle can only belong to one nation at a time right?

> 

You could use single table inheritance (STI).

1. all castles are stored in one table (‘castles’) which has all the  
required attributes for all types of castles.
2. in app/models you’ll have your Castle model and then you’ll have a  
CastleA,B and C models like above but you now inherit from Castle:  
class CastleA \< Castle  
end
3. You include a ‘type’ field in ‘castles’ table which is a string and  
is  
the same as the class name of the castle. (ie for an A-type it would be  
‘CastleA’)

This is a tidy way to handle your castles as everything is in one table.  
It  
does mean that not all attributes will be used by a given castle  
instance if  
you have specialised attributes; these will probably be null so I don’t  
think the wastage will be much.

> and second question is  
> when nation deleted, CastleA, CastleB have to be deleted but not for  
> the castleC type.  
> how can I do?

I’m never good at this stuff. But you could do it with a callback in  
your  
Nation model:

class Nation \< ActiveRecord::Base  
has\_many :castles  
before\_destroy do |nation|  
Castle.destroy\_all “nation\_id=#{nation.id} and type!=‘#{CastleC}’”  
end  
end

There’s probably a sexier way to do that.

–  
Daniel B.

[http://blog.web17.com.au](http://blog.web17.com.au)

> **[GitHub - danielbush/sifs: The Simple Include-File System (for bash)](https://github.com/danielbush/sifs/tree/master)**
>
> master

> **[danielbush - Overview](https://github.com/danielbush)**
>
> danielbush has 33 repositories available. Follow their code on GitHub.
