On 26.01.2011 14:55, Mauro wrote:
Then comes another user and I must give bags of type “dry”, then comes
another user and I must give bags of all three types.
At date 2011-01-02 it can be the same story, and so on.
At the end I must know for every user how much deliveries, the dates
of every single delivery and for every single delivery what type of
bags I gave to him.
Now we have a story! I think we should rephrase some terms to make stuff
easier.
Lets say, a single customer (class Customer) comes, and what he does is
to place an order (class Order).
class Customer < ActiveRecord::Base
has_many :orders
class Order < ActiveRecord::Base
belongs_to :customer
customer has the typical attributes like name and address. Orders
attribute would be the order date.
Now an order consists of multiple order items (class OrderItem), and
each order item refers to the product (class Product).
class Customer < ActiveRecord::Base
has_many :orders
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_items
has_many :products, :through => :order_items
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
has_one :customer, :through => :order
class Product < ActiveRecord::Base
has_many :order_items
has_many :orders, :through => :order_items
Product has typical attributes like name, price and weight per piece,
and so on. OrderItem has attributes like number of items ordered, actual
price granted for that products item (after discount), and so on. You
will want to extend Order as well to cover maybe a total price, total
discount, or have some delivery tracking.
This can be easily mapped or renamed to what you need. You have to keep
in mind that you line up all your orders in a separate table, but this
table
does not contain the actual ordered items as these are again in a
separate table, OrderItem, which by itself just refers to the actual
product.
Matthias