Has_many :through best practices

2011/1/26 Matthias W. [email protected]:

On 26.01.2011 13:20, Mauro wrote:

On 26 January 2011 11:58, Colin L.[email protected] wrote:

That means that one delivery is to multiple users, is that what you want?

Yes in a single day I can do a lot of deliveries to different users.

Colin meant that any single delivery is designed to be addressed to multiple
users, and I doubt that this is what you want.

In date 2011-01-01 I can deliver bag type “plastic” to user1,user2 and
user3 or in the same date I can deliver bag type “plastic”, “wet” and
“dry” to user1, user2 and user3, or I can do the same types of
deliveries only to one user.

On 26 January 2011 13:55, Mauro [email protected] wrote:

same delivery? If that is the case in which table are you proposing
to save which bag(s) go to which user?

We are confusing and it 's my fault because of my poor English.

That is understandable when you are not using your own language.
However it does mean that you must read every reply very carefully in
order to try to understand what is being said.

Back to the top.
I have a lot of users and I have three bag types: “plastic”, “wet” and “dry”.
At date 2011-01-01 a user comes to me and I must deliver to him some
bags of type “plastic”, I give bags to him I must annotate the date of
deliver and what type of bags I gave.

So do you count that as one delivery, one user, three bags?

Not matter the number of bags.
At the same date another user comes to me and I must give to him some
bags of type “plastic” and “wet”, I give the bags I must annotate the
date of deliver and what type of bags I gave.

Another delivery, different user, and so on?

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.

One more point of confusion I think. When you talk about your bags
table is that a record for each bag that will exist or is it just a
table of bag types, in which case make the class BagType and table
bag_types

Colin

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

2011/1/26 Matthias W. [email protected]:

At the same date another user comes to me and I must give to him some
easier.

belongs_to :customer
has_many :orders, :through => :order_items
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.

I thought for some days and I think this is the best solution.