<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Creating Many Models in One Form]]></title>
		<link>http://railsforum.com/viewtopic.php?id=1063</link>
		<description><![CDATA[The most recent posts in Creating Many Models in One Form.]]></description>
		<lastBuildDate>Sat, 26 Dec 2009 06:36:43 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=115343#p115343</link>
			<description><![CDATA[<p>I had trouble getting this to work, but then I found an example app and was able to learn from it. Here&#039;s the link:http://github.com/alloy/complex-form-examples</p><p>Highly suggest you guys download and play around with it.</p>]]></description>
			<author><![CDATA[dummy@example.com (senthilnambi)]]></author>
			<pubDate>Sat, 26 Dec 2009 06:36:43 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=115343#p115343</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=115297#p115297</link>
			<description><![CDATA[<p>I am having trouble creating a parent/child single form when using formtastic. Can anyone provide any insight into how to do this?</p><p>I am trying to convert my existing code (based on Ryan Bates&#039; railscast - Complex Forms).</p><p>A Job has_many Questions.</p><p>I am trying to incorporate into the create job form the ability to create some questions at the same time.</p><p>#jobs_controller.rb <br />&nbsp; def new<br />&nbsp; &nbsp; @job = Job.new<br />&nbsp; &nbsp; 3.times {@job.questions.build}<br />&nbsp; end</p><p># jobs/new.html.erb<br />&lt;% semantic_form_for @job do |f| %&gt;<br />&nbsp; &lt;% f.inputs do %&gt;<br />&nbsp; &nbsp; &lt;%= f.input :title %&gt;<br />&nbsp; &lt;% end %&gt;<br />&nbsp; <br />&nbsp; &lt;% @job.questions.each_with_index do |q, i| %&gt;<br />&nbsp; &nbsp; &lt;% f.semantic_fields_for :question_attributes do |qf| %&gt;<br />&nbsp; &nbsp; &nbsp; &lt;% qf.inputs do %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;%= qf.input :job_id, :as =&gt; :hidden, :value =&gt; &#039;job[id]&#039; %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;%= qf.input :question %&gt;<br />&nbsp; &nbsp; &nbsp; &lt;% end %&gt;<br />&nbsp; &nbsp; &lt;% end %&gt;<br />&nbsp; &lt;% end %&gt;<br />&nbsp; <br />&nbsp; &lt;%= f.buttons %&gt;<br />&lt;% end %&gt;</p><p>In Ryan&#039;s cast the submitted objects should take the form<br />job[question_attributes][][question] but I do not know how to convert them into that array of questions.</p><p>Thanks =~ Jeremiah</p>]]></description>
			<author><![CDATA[dummy@example.com (jmesserer)]]></author>
			<pubDate>Wed, 23 Dec 2009 22:25:32 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=115297#p115297</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87920#p87920</link>
			<description><![CDATA[<p>In your form:<br /><pre name="code" class="ruby:nogutter">Register Number of student&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Attendance<br />==========================&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =============<br />012345A&nbsp; &nbsp;*Taken from @students array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1# Input from user<br />012346B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0#<br />083643H &lt;----I need to merge these 2 values and save them---&gt; 1#</p><p>Enter Date :[________]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Submit</pre><br />It looks like a list of RegisterRecords where every RegisterRecord have a ONE &#039;record_date&#039;. In your form I can see one and the only one DATE_FIELD.The question is how can you guess for what registerRecord the entered date will be? That&#039;s the first.<br />Second - ONE REgisterRecord have ONE or MANY Attendances. That&#039;s why you can&#039;t show all the Attendances just in face of one RegisterRecord and that&#039;s why you&#039;ll have a lot of &#039;double&#039; lines for the same RegisterRecord(differed in Attendans value only). Technically it&#039;s possible by using some JavaScript plugins proposed by jQuery, Mootools, etc.I&#039;d better put a link &#039;Attendances on the every RegisterRecord line. By clicking on that link you&#039;l get</p>]]></description>
			<author><![CDATA[dummy@example.com (Javix)]]></author>
			<pubDate>Mon, 16 Feb 2009 12:42:46 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87920#p87920</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87906#p87906</link>
			<description><![CDATA[<p>Hi</p><p>Actually I&#039;m hoping to save a RegisterRecord by the date the user input and many Attendances for this RegisterRecord will be saved.<br />I can&#039;t merge the values of Student register number and the corresponding attendance.There for I&#039;m stuck at this point.</p><p>def create</p><p>@register_record=RegisterRecord.new({&quot;register_id&quot;=&gt;params[:Reg_id],&quot;entered_by&quot;=&gt;session[:user_id],&quot;record_date&quot;=&gt;params[:Record_date]})<br /> if @register_record.save<br />&nbsp; &nbsp; &nbsp; &nbsp; flash[:notice] = &#039;Register Record Successfully saved&#039;<br />&nbsp; end<br />&nbsp; end</p><p>Saving attendances is yet to be implemented.Which is the problem here <img src="http://railsforum.com/img/smilies/sad.png" width="15" height="15" alt="sad" /></p>]]></description>
			<author><![CDATA[dummy@example.com (goodnews)]]></author>
			<pubDate>Mon, 16 Feb 2009 10:10:32 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87906#p87906</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87903#p87903</link>
			<description><![CDATA[<p>In your form you call a &#039;create&#039; action but in your controller you showed me only the &#039;new&#039; action. What does your &#039;create&#039; action do?</p>]]></description>
			<author><![CDATA[dummy@example.com (Javix)]]></author>
			<pubDate>Mon, 16 Feb 2009 08:31:57 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87903#p87903</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87895#p87895</link>
			<description><![CDATA[<p>Hi Javix,<br />Thanks for your reply.. I dont think I was clear the 1st time. The problem is as follows.</p><p>My 2 models are <br />&nbsp; &nbsp; 1.RegisterRecord<br />&nbsp; &nbsp; 2.Attendance</p><p>class RegisterRecord &lt; ActiveRecord::Base<br />&nbsp; has_many :attendances<br />&nbsp; belongs_to:register<br />end</p><p>class Attendance &lt; ActiveRecord::Base<br />&nbsp; belongs_to :register_record<br />end</p><p>Now I need to save a register record by a &#039;date&#039; selected by the user. A RegisterRecord belongs to a Register.A Register belongs to class.<br />In the new action of following code I get all students enrolled in the class to @students array.And I build an &#039;Attendance&#039; for each student. </p><p>class RegisterRecordsController &lt; ApplicationController<br />&nbsp; layout&#039;profile&#039;<br />&nbsp; def new<br />&nbsp; &nbsp; @register_id=@id<br />&nbsp; &nbsp; @record=RegisterRecord.new<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @students=StudentClass.new().get_allstudents_regno(session[:current_class])<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @rec_no=@students.size<br />&nbsp; &nbsp; @rec_no.times{@record.attendances.build}<br />&nbsp; end</p><p>Now the form which is used to save the RegisterRecord has following requirements.</p><p>Register Number of student&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Attendance<br />==========================&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =============<br />012345A&nbsp; &nbsp;*Taken from @students array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1# Input from user<br />012346B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0#<br />083643H &lt;----I need to merge these 2 values and save them---&gt; 1#</p><p>Enter Date :[________]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Submit</p><p>I need to create &#039;one&#039; textfeild for each student.But the following code generates many textfeilds because of the nested for. But I cant figure out any other way also.</p><p>&lt;%if @students.blank? %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td class=&quot;infoMessage&quot;colspan=&quot;2&quot;&gt;&lt;center&gt;No students enrolled in this class.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Make each student enrol for this class..&lt;/center&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%else%&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;%form_tag({:controller =&gt; &quot;register_records&quot;, :action =&gt; &quot;create&quot;}, :method =&gt; &quot;post&quot;) do%&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;tr&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;%for student in @students&nbsp; %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%=label :Reg_number,student&nbsp; %&gt; &lt;/td&gt;<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%@record.attendances.each_with_index do |attendns,index|%&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;% fields_for &quot;attendances[#{index}]&quot;, attendns do |f| %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%= f.text_field :attendance %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%f.hidden_field:Reg_no,:value=&gt;student%&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/td&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;% end %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%end%&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/tr&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;%end%&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td colspan=&quot;1&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Select Date:&lt;/td&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td colspan=&quot;1&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%= calendar_date_select_tag :Record_date,&#039;click on the icon &#039; %&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/td&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&nbsp; colspan=&quot;2&quot;&gt; &lt;%= image_submit_tag(&quot;SavecommonButton3Hover.gif&quot; , :align =&gt; &quot;right&quot;, :height =&gt; 25, :width =&gt; 59, :border =&gt; 0)%&gt;&lt;/td&gt;&nbsp; &lt;/tr&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%end%&gt;<br />&nbsp; &nbsp;</p><p>I need to merge the value of attendance to each corresponding student registration number.<br />One RegisterRecord will be saved with the date.<br />For the id of that RegisterRecord many Attendances will be saved in attendances table.</p><p>class CreateAttendances &lt; ActiveRecord::Migration<br />&nbsp; def self.up<br />&nbsp; &nbsp; create_table :attendances do |t|<br />&nbsp; &nbsp; &nbsp; t.integer :register_record_id<br />&nbsp; &nbsp; &nbsp; t.string :registration_no<br />&nbsp; &nbsp; &nbsp; t.boolean :attendance</p><p>&nbsp; &nbsp; &nbsp; t.timestamps<br />&nbsp; &nbsp; end</p><p>class CreateRegisterRecords &lt; ActiveRecord::Migration<br />&nbsp; def self.up<br />&nbsp; &nbsp; create_table :register_records do |t|<br />&nbsp; &nbsp; &nbsp; t.integer :register_id<br />&nbsp; &nbsp; &nbsp; t.integer :entered_by<br />&nbsp; &nbsp; &nbsp; t.date :record_date<br />&nbsp; &nbsp; &nbsp; t.boolean :approval<br />&nbsp; &nbsp; &nbsp; t.integer :validated_by</p><p>&nbsp; &nbsp; &nbsp; t.timestamps<br />&nbsp; &nbsp; end<br />&nbsp; end</p><p>Thankx in advance.&nbsp; </p><p>P:S: I tried to create a hash and hash the Student registration number and the attendance value which will be a user input. But I got a hash with only the registration numbers hashed. The attendance values did not get hashed. They remained blank.<br />And the hash looked something like</p><p>{&quot;012345A&quot;=&gt;&quot;&quot;,&quot;012345A 012346B&quot;=&gt;&quot;&quot;,&quot;012345A 012346B 083643H&nbsp; &quot;=&gt;&quot;&quot; }</p>]]></description>
			<author><![CDATA[dummy@example.com (goodnews)]]></author>
			<pubDate>Mon, 16 Feb 2009 04:21:19 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87895#p87895</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87885#p87885</link>
			<description><![CDATA[<div class="quotebox"><cite>goodnews wrote:</cite><blockquote><p>Hi<br />Can someone help me to figure this out...</p><p>I have 2 models called Register_record and Attendance.A Register_record has many Attendances.</p><p>I have a form which allows user to select the date, and a Register_record will be saved from that date.For that register_record many attendances have to be saved.</p><p>class RegisterRecordsController &lt; ApplicationController<br />&nbsp; layout&#039;profile&#039;<br />&nbsp; def new<br />&nbsp; &nbsp; @register_id=@id<br />&nbsp; &nbsp; @record=RegisterRecord.new<br />&nbsp; &nbsp; <br />@students=StudentClass.new().get_allstudents_regno(session[:current_class])<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @rec_no=@students.size<br />&nbsp; &nbsp; @rec_no.times{@record.attendances.build}<br />&nbsp; end</p><p> For each student in @students array I have to display the name and display a text field to insert attendance in front of the name. </p><p>After this we have to save the students name and the corresponding attendance value in Attendances table from the Register_record Id.</p><p>I tried to hash the name of student and the attendance value(user input from text field)but didnt work.<br />Can someone suggest a solution for this.</p><p>Thanx</p></blockquote></div><p>Hi!<br />First of all, could you send t stack trace of errors that u have.<br />Than check your routes. It&#039;s not so clear what your models are. Post them too.<br />Personally whet I have the same situation like something can have O or many things, I first of all put in routes as:<br /><pre name="code" class="ruby:nogutter">map.resources :register_records , :has_many=&gt; :attendances</pre><br />Then you attach the show action created by Rails scaffold generato (if u use it) you add a list (or table or whatever u want) of your &#039;attendances&#039; and a link to add a new one like this:<br /><pre name="code" class="ruby:nogutter">&lt;%= link_to &quot;Add a attendance&quot;, new_register_record_attendance_url(@register_record) %&gt;</pre><br />Your instance variable will be available from you &#039;show&#039; action in RegisterRecordController and you just call a new action but this time in AttendancesController.<br />In you new.htl.erb you declare your form like that:<br /><pre name="code" class="ruby:nogutter">&lt;% form_for [@register_record, @attendance] do |form| %&gt;<br />etc...<br />&lt;%end%&gt;</pre><br />In your AttendencesController you add:<br /><pre name="code" class="ruby:nogutter">before_filter :find_register_record, :except =&gt; &quot;index&quot;</p><p>def new<br />&nbsp; &nbsp; @attendance = Attendance.new<br />&nbsp; end</p><p>&nbsp; def edit<br />&nbsp; &nbsp; @attendance = @register_record.attendances.find(params[:id])<br />&nbsp; end</p><p>&nbsp; def create<br />&nbsp; &nbsp; @attendance = Attendance.new(params[:attendance])<br />&nbsp; &nbsp; if (@register_record.attendances &lt;&lt; @attendance)<br />&nbsp; &nbsp; &nbsp; redirect_to register_record_url(@register_record)<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp; &nbsp; render :action =&gt; :new<br />&nbsp; &nbsp; end<br />&nbsp; end<br />private<br />&nbsp; def find_register_record<br />&nbsp; &nbsp; id = params[:register_record_id]<br />&nbsp; &nbsp; return(redirect_to(register_records_url)) unless id<br />&nbsp; &nbsp; @register_record = RegisterRecord.find(id)<br />&nbsp; end</pre><br />Adapt your &#039;update&#039; method in the same way as &#039;new&#039;.</p>]]></description>
			<author><![CDATA[dummy@example.com (Javix)]]></author>
			<pubDate>Sun, 15 Feb 2009 21:28:30 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87885#p87885</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=87864#p87864</link>
			<description><![CDATA[<p>Hi<br />Can someone help me to figure this out...</p><p>I have 2 models called Register_record and Attendance.A Register_record has many Attendances.</p><p>I have a form which allows user to select the date, and a Register_record will be saved from that date.For that register_record many attendances have to be saved.</p><p>class RegisterRecordsController &lt; ApplicationController<br />&nbsp; layout&#039;profile&#039;<br />&nbsp; def new<br />&nbsp; &nbsp; @register_id=@id<br />&nbsp; &nbsp; @record=RegisterRecord.new<br />&nbsp; &nbsp; <br />@students=StudentClass.new().get_allstudents_regno(session[:current_class])<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @rec_no=@students.size<br />&nbsp; &nbsp; @rec_no.times{@record.attendances.build}<br />&nbsp; end</p><p> For each student in @students array I have to display the name and display a text field to insert attendance in front of the name. </p><p>After this we have to save the students name and the corresponding attendance value in Attendances table from the Register_record Id.</p><p>I tried to hash the name of student and the attendance value(user input from text field)but didnt work.<br />Can someone suggest a solution for this.</p><p>Thanx</p>]]></description>
			<author><![CDATA[dummy@example.com (goodnews)]]></author>
			<pubDate>Sun, 15 Feb 2009 15:57:45 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=87864#p87864</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=83319#p83319</link>
			<description><![CDATA[<p>All this is enogh clear now. But recently I got a project for which I have no idea how to create multiple models in one form. The problem that there is a lot of many-to-many relations. And I&#039;m blocked with the last one. So I have 2 tables: Studies and Countries joint as may-to-many via a join table CountryStudies. But the main problem is that this CountryRequirements is joint by many-to-many to an other table - Chemicals via a joint table ChemicalsCountryStudies. So we have something like this:<br />study.rb<br /><pre name="code" class="ruby:nogutter">has_many :country_studies<br />has_many :countries, :through=&gt;:country_studies</pre><br />country.rb<br /><pre name="code" class="ruby:nogutter">has_many :country_studies<br />has_many :studies, :through=&gt;:country_studies</pre><br />country_study.rb<br /><pre name="code" class="ruby:nogutter">belongs_to :study<br />belongs_to :country<br />has_many :chemicals_country_studies<br />has_many :chemicals, :through=&gt;:chemicals_country_studies</pre><br />chemicals_country_study.rb:<br /><pre name="code" class="ruby:nogutter">belongs_to :country_study<br />belongs_to :chemicals</pre><br />chemical.rb<br /><pre name="code" class="ruby:nogutter">has_many :chemicals_country_study<br />ha_many :country_studies, :through=&gt;:chemicals_country_studies</pre><br />I should always start by creating a Study. Chemicals list already exists in the moment of creation. How could I do that. The main information is encoded on Study page. Thanks</p>]]></description>
			<author><![CDATA[dummy@example.com (Javix)]]></author>
			<pubDate>Sun, 21 Dec 2008 21:37:41 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=83319#p83319</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=79791#p79791</link>
			<description><![CDATA[<p>I have been trying to do something very similar.</p><p>Take a look at:</p><p><a href="http://www.ruby-forum.com/topic/165891">http://www.ruby-forum.com/topic/165891</a></p>]]></description>
			<author><![CDATA[dummy@example.com (arichards)]]></author>
			<pubDate>Sat, 08 Nov 2008 21:20:21 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=79791#p79791</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=69208#p69208</link>
			<description><![CDATA[<p>how can i dynamically generate the form helper tags? In the sense, how can i make text_field or text_area a variable and generate the form using that variable?</p>]]></description>
			<author><![CDATA[dummy@example.com (yourstruly_vinay)]]></author>
			<pubDate>Tue, 22 Jul 2008 12:36:12 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=69208#p69208</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=54142#p54142</link>
			<description><![CDATA[<p>I&#039;m sorry. I&#039;d love to be able to help you directly, but my suggestion is actually that you take a few steps back and refactor lots of stuff.</p><p>Check out the api on the form helpers... especially collection_select:<br /><a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000941">http://api.rubyonrails.org/classes/Acti &#133; ml#M000941</a></p><p>Also, it&#039;s best - if you are going to work with rails - to follow rails conventions. Don&#039;t fight it, go with it. For example, in rails, tables have plural names by convention. That&#039;s a start.</p><p>Check out the api on model associations and the handy methods that get thrown in with each. In your app structure, you have an natural fit for a :has_many :through relationship. A student &quot;has many&quot; classes&quot; through &quot;enrollments&quot; ...and vice versa. I suggest you set it up this way. Create a &quot;students&quot; table with student info, a &quot;classes&quot; table with information about each class, and a join table called &quot;enrollments&quot; which joins a student to a class.</p><p><a href="http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001103">http://api.rubyonrails.com/classes/Acti &#133; ml#M001103</a></p><p>You could then create a &quot;grades&quot; table and associate it with your &quot;enrollments&quot; table. So, again, a &quot;student&quot; would have many &quot;grades&quot; through &quot;enrollments&quot;... and a class would have many grades through enrollments (allowing you to easily average grades by class, for example).</p><p>That&#039;s my suggestion. It&#039;s not going to help you all that much to rush just for the sake of rushing. Dig in... and enjoy it!</p>]]></description>
			<author><![CDATA[dummy@example.com (ricsrock)]]></author>
			<pubDate>Fri, 15 Feb 2008 20:44:28 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=54142#p54142</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=54051#p54051</link>
			<description><![CDATA[<p>here&#039;s the relation. click on this link (picture) -&gt; <a href="http://i250.photobucket.com/albums/gg268/veil007/db.jpg">http://i250.photobucket.com/albums/gg268/veil007/db.jpg</a></p><p>here&#039;s the print screen image of my addgrade.rhtml. when i click &quot;Add Grade&quot; button, it will create 3 new rows of record in gradebook table in my database.<br />--&gt; <a href="http://i250.photobucket.com/albums/gg268/veil007/GRADEBOOK.jpg">http://i250.photobucket.com/albums/gg26 &#133; DEBOOK.jpg</a></p><p>hope anyone willing to share their knowledge </p><p>#FOR def egradebook<br />&lt;a name=&quot;TemplateInfo&quot;&gt;&lt;/a&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h1&gt;E-Gradebook&lt;/h1&gt;&lt;br&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h3&gt; Select Which Class &amp; Subject You Would Like To Update &lt;/h3&gt;&lt;br&gt;<br />&lt;%= start_form_tag :action =&gt; &#039;addgrade&#039; %&gt;<br />&lt;label for = &quot;class1_id&quot;&gt;Select Class&lt;/label&gt;<br />&lt;select name=&quot;class_1[id]&quot;&gt;<br />&lt;option value=&quot;&quot; selected=&quot;selected&quot; &gt;Please Select Class&lt;/option&gt;<br />&lt;%@class1s.each do |class1| %&gt;<br />&lt;option value=&quot;&lt;%=class1.id%&gt;&quot;&gt; &lt;%=class1.classname%&gt; &lt;/option&gt;<br />&lt;%end%&gt;<br />&lt;/select&gt;&lt;br&gt; &lt;br&gt;</p><br /><p>&lt;label for = &quot;subject_id&quot;&gt;Select Subject&lt;/label&gt;<br />&lt;select name=&quot;subject[id]&quot;&gt;<br />&lt;option value=&quot;&quot; selected=&quot;selected&quot; &gt;Please Select Subject&lt;/option&gt;<br />&lt;%@subjects.each do |subject| %&gt;<br />&lt;option value=&quot;&lt;%=subject.id%&gt;&quot;&gt; &lt;%=subject.subjectname%&gt; &lt;/option&gt;<br />&lt;%end%&gt;<br />&lt;/select&gt;&lt;br&gt;&lt;br&gt;</p><p>&lt;%= submit_tag &quot;Submit&quot;%&gt;</p><p>&lt;%end_form_tag %&gt;&lt;br&gt;<br />--------------------------------------------</p><p>#FOR def addgrade<br />&lt;a name=&quot;TemplateInfo&quot;&gt;&lt;/a&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h1&gt;Update E-Gradebook&lt;/h1&gt;&lt;br&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;table width=&quot;100%&quot; border=&quot;0&quot;&gt;<br />&nbsp; &lt;tr&gt;&lt;%=form_tag :action =&gt; &#039;creategrade&#039;, :id =&gt; @gradebook%&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;22%&quot;&gt;&lt;strong&gt;Class:&lt;/strong&gt; &lt;%=params[:class_1][:id]%&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td colspan=&quot;2&quot;&gt;&lt;strong&gt;Subject:&lt;/strong&gt; &lt;%=params[:subject][:id]%&gt; &lt;/td&gt;<br />&nbsp; &lt;/tr&gt;<br />&nbsp; &lt;tr&gt;<br />&nbsp; &nbsp; &lt;td&gt;&lt;strong&gt;Semester:&lt;/strong&gt; &lt;%= text_field &#039;gradebook&#039;, &#039;semester&#039; , :size =&gt; 3 %&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;20%&quot;&gt;&lt;strong&gt;Year:&lt;/strong&gt; &lt;%= text_field &#039;gradebook&#039;, &#039;year&#039; , :size =&gt; 5 %&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;58%&quot;&gt;&lt;label for = &quot;teacher_id&quot;&gt;&lt;strong&gt; Select Teacher: &lt;/strong&gt;&lt;/label&gt;<br />&lt;select name=&quot;gradebook[teacher_id]&quot;&gt;<br />&lt;option value=&quot;&quot; selected=&quot;selected&quot; &gt;Please Select Teacher Name&lt;/option&gt;<br />&lt;%@teachers.each do |teacher| %&gt;<br />&lt;option value=&quot;&lt;%=teacher.id%&gt;&quot;&gt; [&lt;%=teacher.user.id%&gt;] &lt;%=teacher.user.firstname%&gt; &lt;%=teacher.user.lastname%&gt; &lt;/option&gt;<br />&lt;%end%&gt;<br />&lt;/select&gt;&lt;/td&gt;<br />&nbsp; &lt;/tr&gt;<br />&nbsp; &lt;tr&gt;<br />&nbsp; &nbsp; &lt;td&gt;&amp;nbsp;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;<br />&nbsp; &lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;br&gt;<br />&lt;table width=&quot;100%&quot; border=&quot;0&quot;&gt;<br />&nbsp; &lt;tr&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;76%&quot;&gt;&lt;strong&gt;Student ID &amp; Name &lt;/strong&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;12%&quot;&gt;&lt;div align=&quot;center&quot;&gt;&lt;strong&gt;Mark&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td width=&quot;12%&quot;&gt;&lt;div align=&quot;center&quot;&gt;&lt;strong&gt;Grade&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;<br />&nbsp; &lt;/tr&gt;<br />&nbsp; &lt;tr&gt;&lt;%@students.each do |student|%&gt;<br />&nbsp; &nbsp; &lt;td&gt;[&lt;%=student.user.id%&gt;] &lt;%=student.user.firstname%&gt; &lt;%=student.user.lastname%&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td&gt;&lt;div align=&quot;center&quot;&gt; &lt;%= text_field &#039;gradebook&#039;, &#039;mark&#039; , :size =&gt; 3 %&gt;&lt;/div&gt;&lt;/td&gt;<br />&nbsp; &nbsp; &lt;td&gt;&lt;div align=&quot;center&quot;&gt;&lt;%= text_field &#039;gradebook&#039;, &#039;grade&#039; , :size =&gt; 3 %&gt;&lt;/div&gt;&lt;/td&gt;<br />&nbsp; &lt;/tr&gt;&lt;%end%&gt;<br />&lt;/table&gt;<br />&lt;div align=&quot;center&quot;&gt;&lt;br&gt;<br />&nbsp; &nbsp; &lt;%= submit_tag &quot;Add Grade&quot; %&gt; &lt;%= end_form_tag %&gt;<br />--------------------------------------------</p><p>def creategrade is the action to save the data into the database</p>]]></description>
			<author><![CDATA[dummy@example.com (veil)]]></author>
			<pubDate>Thu, 14 Feb 2008 19:25:44 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=54051#p54051</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=54024#p54024</link>
			<description><![CDATA[<p>Can you post more of your code?</p><p>How are your tables/models structured and related?</p><p>Also, post the view code for this form.</p>]]></description>
			<author><![CDATA[dummy@example.com (ricsrock)]]></author>
			<pubDate>Thu, 14 Feb 2008 15:21:21 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=54024#p54024</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating Many Models in One Form]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=54001#p54001</link>
			<description><![CDATA[<p>hi there.. i have some problem.. im a newbie in rails but i have to do my final year project.<br />can u edit my code here. i cant save the students mark into the gradebook table.</p><p>def egradebook<br />@class1s = Class1.find(:all)<br />@subjects = Subject.find(:all)<br />end</p><p>def addgrade<br />@gradebook = Gradebook.new<br />@students = Student.find(:all, :conditions =&gt; [&quot;class1_id = ?&quot;, params[:class_1][:id]])<br />@teachers = Teacher.find(:all)<br />end</p><p>def creategrade<br />@gradebook.class1_id = params[:class_1][:id]<br />@gradebook.subject_id = params[:subject][:id]<br />@students = Student.find(:all, :conditions =&gt; [&quot;class1_id = ?&quot;, params[:class_1][:id]])<br />@gradebook = Gradebook.new(params[:gradebook])<br />@teacher = Teacher.find(params[:id])<br />##here is the problem. unsure to add students mark and make the output as multiple of rows in gradebook table.<br />if @gradebook.save<br />&nbsp; &nbsp; flash[:notice] = &#039;Gradebook Successfully Added.&#039;<br />&nbsp; &nbsp; redirect_to :action =&gt; &#039;index&#039;<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp;&nbsp; flash[:notice] = &#039;Please Login to Add Grade&#039;<br />&nbsp; &nbsp; &nbsp; render :action =&gt; &#039;login&#039;<br />&nbsp; &nbsp; &nbsp; end<br />end</p><p>hope u can help me asap. thank you.</p>]]></description>
			<author><![CDATA[dummy@example.com (veil)]]></author>
			<pubDate>Thu, 14 Feb 2008 06:56:09 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=54001#p54001</guid>
		</item>
	</channel>
</rss>
