Topic: MVC: Where to build complex object structures?

Hi all

Sorry for my english, I hope you're able to understand what i'm talking about.

I'm currently working on a mid-sized webapp project (eshop) and decided to give rails a try. Up to now I'm pretty happy with my choice.

At the moment, i'm working on a form which edits a rather complex, multi language "Product" object, which (among others) has a polymorphic has_many relationship with "AttributeValue" objects and a has_many relationship with "Dictionary" objects. Additionaly, the "AttributeValue" objects have default values coming from "AttributeDefaultValue" objects. The "AttributeValue" objects (depending on the type) may have has_many relationships with "AttributeValueDictionary" objects and so on.

The reason for this mess is that there must be different types of attributes (select, text, number, etc...). The actual "instances" of the attributes are specified dynamically in the backend of the shop (e.g. Fabric : select, Origin : text, Weight : number).

The editing / creating of multiple models in one form is not a problem, thanks to the great tutorial dealing with this issue which I have found in this forum.

My question: building the "product" object described above when creating a new instance of "product" is getting rather complex, in pseudo code something like:

def new
  @prod =

  Lang.find_all.each do |l|
    @prod.dicts << => l)

  AttrType1.find_all.each do |a|
      v = => a)
      Lang.find_all.each do |l|
        dict = => l)
        dflt = AttrType1ValDefaults.find_by_attr_id_and_lang_id(a, ...).value
        dict.value = dflt
        v.dicts << dict
    @prod.attrs << v

  AttrTypeYouGetThePicture.find_all.each ...


What would be good application design here? To put the code into a protected method of the controller? Into the constructor of "product"?

Does anybody have experience with a simmilar problem? How did you solve it?

Thanks in advance

Re: MVC: Where to build complex object structures?

Can you post each of the models involved so I can see the exact relationships? It will be easier to understand that way. No need to post all of the methods, just the associations.

If you could post the form view for creating a new product, that would be helpful too (if you have already made it that is).

Railscasts - Free Ruby on Rails Screencasts