Search This Blog

Thursday, December 16, 2010

How to associate descendant models to a parent.

Programmer Question

Hi, I have the following models:



Account
has_many :libraries

Library
has_many :topics
belongs_to :account

Topic
has_many :functions
belongs_to :library

Function
has_one :example
belongs_to :topic

Example
belongs_to :function


I would like to be able to able to do things such as:



some_account.libraries
some_account.topics
some_account.functions
some_account.examples


In addition, I would like to be able to assign an account to a descendant, i.e



some_example.account = some_account
some_function.account = some_account
some_topic.account = some_account
some_library.account = some_account


To give some context:
I am letting a user (Account) create each Library, Topic, Function, Example. record separately. Then a user is free to change how the records are associated: Change the topic of a Function, move a Topic to a different Library, add an example to a function, and so on.



To my understanding no matter what record is created, I would need to assign it to a user (account) so that I can have a list of each Model records that a user has created, as well as prevent other users from seeing stuff that doesn't belong to them



Although I might be overcomplicating, I really don't know :(



Thanks in advance.



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails