# Rails Table Relations questions

**URL:** <https://www.ruby-forum.com/t/rails-table-relations-questions/158266>\
**Category:** Rails\
**Created:** [January 21, 2009, 10:20am UTC](https://www.ruby-forum.com/t/rails-table-relations-questions/158266 "2009-01-21T10:20:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sickboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sickboy](https://www.ruby-forum.com/u/sickboy)\
**Post date:** [January 21, 2009, 10:20am UTC](https://www.ruby-forum.com/t/rails-table-relations-questions/158266/1 "2009-01-21T10:20:58Z")

</div>

Hello,

I’m breaking my head atm about table relations / associations in  
Rails.

The idea is this: Collections has many mods, mods have many folders,  
folders have many items.  
A collection can have many different versions, the mods associated  
with a collection, should ‘have the same version’.  
Changelogs are linked to Version, but should differ per Mod.

If I work with ActiveScaffold, things become quite a mess, and I can  
for instance create a Changelog entry, for a mod that is not part of  
the chosen Collection, and so on.

This is basically what I have atm:  
class Collection \< ActiveRecord::Base  
has\_many :mods  
has\_many :versions  
end

class Version \< ActiveRecord::Base  
belongs\_to :collection  
has\_many :changelogs  
end

class Mod \< ActiveRecord::Base  
belongs\_to :collection  
has\_many :folders  
has\_many :changelogs  
end

class Changelog \< ActiveRecord::Base  
belongs\_to :version  
belongs\_to :mod  
end

class Folder \< ActiveRecord::Base  
belongs to :mod  
has\_many :folders  
end

class Item \< ActiveRecord::Base  
belongs to :folder  
end

Anybody any ideas? Cheers

---

<div class="post-metadata">

**Author:** ![sickboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sickboy](https://www.ruby-forum.com/u/sickboy)\
**Post date:** [January 21, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/rails-table-relations-questions/158266/2 "2009-01-21T12:12:10Z")

</div>

On Tue, Jan 20, 2009 at 4:14 PM, Sickboy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Changelogs are linked to Version, but should differ per Mod.
> 
> class Item \< ActiveRecord::Base  
> belongs to :folder  
> end
> 
> Anybody any ideas? Cheers
> 
> >

If I’ve understood your problem correctly, I would map the core  
relationships as you have described them and then use validation to  
ensure  
the business rules such as a mod of a collection must be of the same  
version  
as the collection.  
You can use the validation method or perhaps before\_save callbacks  
depending  
on which suits what you’re doing better.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain
