views:

56

answers:

1

Hi, I have a situation where I have Products, Suppliers, ShoppingLists and Valuations.

A shopping_list consist of many valuations each with a product, an specific supplier and a price.

My models are as follows:

class Product < ActiveRecord::Base
  has_many :valuations
  has_many :shopping_lists, :through => :valuations
end

class Supplier < ActiveRecord::Base
  has_many :valuations
  has_many :shopping_lists, :through => :valuations
end

class ShoppingList < ActiveRecord::Base
  has_many :valuations
  has_many :products, :through => :valuations
  has_many :suppliers, :through => :valuations
end

class Valuation < ActiveRecord::Base
  belongs_to :product
  belongs_to :supplier
  belongs_to :shopping_list
end

My routes.rb is:

  map.resources :shopping_lists do |shopping_list|
    shopping_list.resources :valuations
  end

  map.resources :product

  map.resources :supplier

I wonder if this could be the best solution, anyway what I want is that the user can create as many lists as he wish, each with several valuations.

The first time a shopping list is created its also filled with one valuation at least. Then, the user can add/remove valuations to/from the shopping_list.

I would like a simple and elegant solution, without Ajax callbacks.

What is the best way to do this, from the controllers/views/routes perspectives? Or should I completley change my schema ?

Thanks!

A: 

Just found two excelent resources from Ryan Bates:

http://asciicasts.com/episodes/196-nested-model-form-part-1

http://asciicasts.com/episodes/197-nested-model-form-part-2

Let's see if that do the job!

// UPDATE: Worked great!

benoror