# How do you populate a collection in a form from a dependent table?

**URL:** <https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013>\
**Category:** Rails\
**Created:** [April 28, 2008, 5:21am UTC](https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013 "2008-04-28T05:21:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [April 28, 2008, 5:21am UTC](https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013/1 "2008-04-28T05:21:10Z")

</div>

Hi,

I’m working on a food program. The user defines foods, enters their  
calories, etc. Then the user goes into a day view with defines a meal  
by selected the foods from a drop down list.

So I have a foods controller, a meals controller.

I have the meals model defined with has\_many :foods

And I have the foods controller defined with belongs\_to :meal

But when I’m not sure when I create the form for it:

# New meal

\<%= error\_messages\_for :meal %\>

\<% form\_for(@meal) do |f| %\>

**Time**  
 \<%= f.datetime\_select :time %\>

**Food**  
 \<%= f.text\_select :food %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, meals\_path %\>

The issue is I need f.text\_select and the options available to the  
user to come from the food table. Anybody know how that would look in  
code?

Thanks,

Ron

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [April 28, 2008, 6:02am UTC](https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013/2 "2008-04-28T06:02:46Z")

</div>

I assume you meant meals has\_many :foods and food belongs\_to :meal in  
the models…

What you need is some type of relation table inbetween those two, since  
an individual food could appear in any number of meals, could it not?

A simple has\_and\_belongs\_to\_many relationship would require a  
foods\_meals table like:

def self.up  
create\_table :foods\_meals, :id =\> false do  
t.column :food\_id, :integer  
t.column :meal\_id, :integer  
end  
end

with a has\_and\_belongs\_to\_many :foods in the meal model, and the  
converse in the food model. Check the rails wiki for examples of HABTM  
usage.

Another alternative is a full join table, I dunno, like ‘servings’.  
def self.up  
create\_table :servings do  
t.column :food\_id, :integer  
t.column :meal\_id, :integer  
\* whatever other columns you’d like to track, ounces (aren’t all  
those calories per serving size?)  
end  
end

then the models would look something like

meal  
has\_many :servings  
has\_many :foods, :through =\> :servings

food  
has\_many :servings  
has\_many :meals, :through =\> :servings

serving  
belongs\_to :meal  
belongs\_to :food

At the risk of writing a dissertation on the subject, google for “rails  
has\_many through” for a plethora of info and how-to’s

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [April 28, 2008, 6:04am UTC](https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013/3 "2008-04-28T06:04:26Z")

</div>

You have to rethink your models. At the very least, you need a many to  
many relation between foods and meals.

Food  
has\_and\_belongs\_to\_many :meals

Meals  
has\_and\_belongs\_to\_many :foods

that will allow you to select the same foods for multiple meals.

for a controller and view for that, you might want to check

[http://wiki.rubyonrails.org/rails/pages/CheckboxHABTM](http://wiki.rubyonrails.org/rails/pages/CheckboxHABTM)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [April 28, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/how-do-you-populate-a-collection-in-a-form-from-a-dependent-table/136013/4 "2008-04-28T12:29:20Z")

</div>

You will probably need some a prototype helper like observe\_field to  
see what you selected in terms of a meal and update the options for  
the foods selection.
