Topic: Whats DRY way of assosiating Account model to all other models?

Hi, to elaborate, I have an Account model, and then the usual kind of stuff like User, Product etc. All Users and all Products belong to an Account. I could add belongs_to to each model and a account_id field to the table, but I will end up with around 20 models which need to be 'bound' to an Account... Is there a more DRY way to do this?

Ideas:
1.) Extend ActiveRecord - problem: not all models are assosiated to an Account, only most of them!

2.) Create a base class which is decended from AR and has the belongs_to, what would I call this class?

3.) Is there an acts_as ? Not found one yet...


Many thanks smile

Re: Whats DRY way of assosiating Account model to all other models?

Actually this is part of a much bigger design process with regards to creating a hosted service. I have my application working so that each person who wishes to use it has to have their own copy of the code, and own database. However if I want to provide my application as a hosted service I need to make sure that each persons account is properly segregated... So if they upload images, they only upload to their folder, and they each have their own cache etc.

Is there any guidence on how to do this?

Many thanks!

Re: Whats DRY way of assosiating Account model to all other models?

I think you can pretty much solve this problem with a polymorphic model.  In that situation the Account model would have two field: accountable_type and accountable_id where it links itself to any number of other models.  Each of the other models would "has_one :accountable" and your association would be complete.
More info:
http://wiki.rubyonrails.com/rails/pages … sociations

Also, you can easily partition your user's data if you create folders based on the id of the user and store all their information in there.

Good luck!

Re: Whats DRY way of assosiating Account model to all other models?

Thanks for the reply. Is a polymorphic association really needed if each model associated with Account is one to many, ie. one Account can have any number of People, Posts etc. Would I not just have account_id in People and Post models? If a Person or Post could belong to many Account then I can see why polymorphic-ism would be good smile

Or do you think I should play it safe and go for polymorphic incase I need many-many in the future?

With regards to segregating account data I'm guessing if I look up the correct paths in the API docs I can simply append the account id to the, for example instead of using public I use public/1/ for Account number 1...

Thanks again.

Re: Whats DRY way of assosiating Account model to all other models?

Actually, polymorphism doesn't allow an account to has_many of anything.  Accounts can only have one related item.  So it sounds just like what you want.

If you needed to get any more complicated than polymorphism though (like if you wanted a many-to-many association) you'd need a join model.  So there'd be Account, Person, Post, but also things like Subscription and PostAccount that had account_id and person_id or post_id in them.  But it doesn't sound like you need that.

I think you're on the right track with the urls :-)