# Adding items in groups in Rails

**URL:** <https://www.ruby-forum.com/t/adding-items-in-groups-in-rails/185594>\
**Category:** Rails\
**Created:** [March 23, 2010, 7:20am UTC](https://www.ruby-forum.com/t/adding-items-in-groups-in-rails/185594 "2010-03-23T07:20:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsquestion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@railsquestion](https://www.ruby-forum.com/u/railsquestion)\
**Post date:** [March 23, 2010, 7:20am UTC](https://www.ruby-forum.com/t/adding-items-in-groups-in-rails/185594/1 "2010-03-23T07:20:57Z")

</div>

My product table is  
id type price location  
1 chips $3 aisle3

I have a question with adding the products in groups.  
There is a quantity field(nonmodel) where the user can enter the  
quantity  
While adding a new product if the user enters:  
type: soda  
quantity: 3

Then 3 records should be created in product model with type= soda like  
the following.  
id type  
2 soda  
3 soda  
4 soda

If user enters  
location: aisle4  
quantity: 2  
Then  
id location  
5 ailse4  
6 ailse4

Can you tell me how to pass the nonmodel field ‘quantity’ to the  
rails(model or controller) and how use it to add the products in  
groups as mentioned above? or should I create a column called quantity  
in my product table? Will the history be updated too for all these new  
records with after\_create filter which I already have created?  
Is there any good tutorial or book which shows how to pass such nonmodel  
html/javascript fields from view to rails and then back to the view?  
Any help will be greatly appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![railsquestion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@railsquestion](https://www.ruby-forum.com/u/railsquestion)\
**Post date:** [March 23, 2010, 9:31am UTC](https://www.ruby-forum.com/t/adding-items-in-groups-in-rails/185594/2 "2010-03-23T09:31:52Z")

</div>

I think it makes most sence to have a “stock” field in the database,  
instead of having one record per stock item.

> how to pass such nonmodel html/javascript fields from view  
> to rails and then back to the view  
> You mean “non database” fields, right?

Add this to the model:  
attr\_accessor :attribute\_name  
Then you can use this field like any other field, except that it wont be  
stored in the database.

---

<div class="post-metadata">

**Author:** ![railsquestion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@railsquestion](https://www.ruby-forum.com/u/railsquestion)\
**Post date:** [March 23, 2010, 9:43am UTC](https://www.ruby-forum.com/t/adding-items-in-groups-in-rails/185594/3 "2010-03-23T09:43:51Z")

</div>

Seems a little odd what you are doing however to answer your question:

You can add a text\_field\_tag or a select\_tag to your form

> **[ActionView::Helpers::FormTagHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001731)**
>
> Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

e.g.

\<%=text\_field\_tag :quantity%\>

You can then get that via params[:quantity] in your controller from  
there you can either pass to your model/business logic class or do a  
simple:

1.upto(params[:quantity].to\_i) do |i|  
Product.create!(params[:product])  
end

Personally though I would seriously consider relooking at the way you  
are working and perhaps normalize your data so you have 3 models:

location  
has\_many :products

type  
has\_many :products

product  
belongs\_to :location  
belongs\_to :type

Then you can do the reverse and use fields\_for to add multiple products  
to a location. Similar to this  
([#196 Nested Model Form Part 1 - RailsCasts](http://railscasts.com/episodes/196-nested-model-form-part-1))

Cheers  
Luke
