views:

194

answers:

3

I have a simple point of sale application written in ruby and rails, and hobo.

Originally intended to be for only one product at the time, now the client wants to add multiple products into the sale model

Besides that i am using brands for categorizing products and in my new sale form i use ajax in order to populate a select product method after selecting the brand in another select menu.

so what i want is to use my current system and just change my new sale form if possibly to add multiple products to a sale

+1  A: 

I guess you have a has_one relation between the sale and one product.
The idea would be to change that relation to a has_and_belongs_to_many.
So in your database, you could have many products for one sale and many sales for one product.

And for the implementation, you can use nested attributes to display the products for one sale and add or remove some.

Damien MATHIEU
A: 

What you're probably looking for is to change the values that get posted from the form from one product to many. Before you probably posted something like this:

product_id=123 product_qty=1

and now you want to post something like this

product_id[0]=123 product_qty[0]=1 product_id[1]=456 product_qty[1]=7

or better yet

product[123].qty=1 product[456].qty=7

In your form, you'll need to create these product variables and make them different. Then post them to the same form you're using, but look at the logs and see how rails is mapping them into the params object. Then in your controller us that mapping to pull out the multiple objects.

I've learned a ton from the railscast screen casts. They don't take long, and watching someone solve problems in < 10 minutes is SO much fun. Try this one http://railscasts.com/episodes/73-complex-forms-part-1

Joe Fair
A: 

i had to create a cart model as a container for products, then procced to use my sale with the cart, and from there further.. validations and stuff had to be reprogrammed

Carlos Barbosa