Forum: Ruby on Rails Rails Table Relations questions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0ffa0effe60c5405cc325b42d9592ac?d=identicon&s=25 Sickboy (Guest)
on 2009-01-21 10:20
(Received via mailing list)
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-21 12:12
(Received via mailing list)
On Tue, Jan 20, 2009 at 4:14 PM, Sickboy <pr@6thsense.eu> 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
This topic is locked and can not be replied to.