# Form to create multiple models

**URL:** <https://www.ruby-forum.com/t/form-to-create-multiple-models/160657>\
**Category:** Rails\
**Created:** [February 18, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/form-to-create-multiple-models/160657 "2009-02-18T14:55:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![diarmuid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@diarmuid](https://www.ruby-forum.com/u/diarmuid)\
**Post date:** [February 18, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/form-to-create-multiple-models/160657/1 "2009-02-18T14:55:19Z")

</div>

Hi

I’m a RoR beginner so excuse any potential stupid questions.

I have 3 models in an attempt to model the following

1. I have a Dayworkout which is a workout for one days workout (day of  
the week independent)
2. Weekworkout is a collection of Dayworkouts, one per day
3. Dateworkout is a separate model just to link a Dayworkout with a  
Weekworkout but including the day-of-the-week info. (ie  
Monday=workout1,Tuesday=workout2,Wednesday=workout1 etc…)

This is following what I think is the recommended approach, ie creating  
a separate model just to link the two other models?

Here are the declarations

# Weekwork

class Weekworkout \< ActiveRecord::Base  
has\_many :dateworkouts  
end  
class CreateWeekworkouts \< ActiveRecord::Migration  
def self.up  
create\_table :weekworkouts do |t|  
t.string :name  
end  
end

# Dayworkout

class Dayworkout \< ActiveRecord::Base  
has\_many :dateworkouts  
end  
class CreateDayworkouts \< ActiveRecord::Migration  
def self.up  
create\_table :dayworkouts do |t|  
t.string :name  
end  
end  
end

#Dateworkout  
class Dateworkout \< ActiveRecord::Base  
belongs\_to :dayworkout  
belongs\_to :weekworkout  
end  
class CreateDateworkouts \< ActiveRecord::Migration  
def self.up  
create\_table :dateworkouts do |t|  
t.integer :dayworkout\_id  
t.integer :weekworkout\_id  
t.string :dayofweek  
end  
end  
end

I think this is ok but my confusion starts when I try to create form to  
create a new weekworkout. My controller is working ok, presenting the  
correct data to the view but in the view I have my problems

# Controller

def new  
@weekworkout = Weekworkout.new  
@coach = current\_user.coach  
# present all the workouts this coach has defined to the view  
@dayworkouts = @coach.find\_workouts  
# all the days of the week  
@daysofweek = %w{Monday Tuesday Wednesday Thursday Friday Saturday  
Sunday}

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @weekworkout }
end

```

end

# view

\<% form\_for(@weekworkout) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

 \<% @daysofweek.each do |day|%\> 

\<%= f.label day %\> \<%= f.collection\_select(:dateworkouts, @dayworkouts, :id, :name )%\>

 \<% end %\> 

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

\<% end %\>

Basically I don’t know what to put in the collection\_select arguments to  
return the data to the controller.  
When I finish I want the following

1. Have created a Weekworkout with a name and 7 dateworkouts
2. Have created 7 dateworkouts with a day of the week, the id of the  
Weekworkout and the id of the dayworkout.

If anything isn’t clear let me know and I will try and clarify.

Any ideas?

D
