Topic: How to insert multiple records at a time in database in rails3

Hai friends i need one help........


I have two tables students and attendences.

In students table i have stdname classname stdID fields

In attendence table i have attdID stdID AttdDate fields.


Now i have view like this:


class: ___________ section: __________ Search(button)

(Then i got list of perticular class and section students)

StudentName       
Raju
RAvi
Rekha



Like this all students now i want to insert the attdstatus to each student like this:


StudentName    AttdStaus    AttdDate
Raju                   _______     ________
Ravi                   _______      ________


Submit(button)


{Then the total students attendence have to insert into the attendence table.}

Please anybody help me by giving hints to  do this? My Email ID is chand5055@gmail.com

Re: How to insert multiple records at a time in database in rails3

As far as I understood, we can divide your problem in 2 steps:

1. Displaying the students list after the search by class and section
2. Updating a list of students by editing their 'AttdStaus' and 'AttdDate' values.

For the first step, just create a page that could look like that:

<% form_tag :action => "results", :method => :get do %>
<p>
<label for="attdstaus">attdstaus:</label>
<%= text_field_tag :attdstaus, params[:attdstaus] %>
</p>
<p>
<label for="attddate">attddate:</label>
<%= text_field_tag :attddate, params[:attddate] %>
</p>
<p>
<%= submit_tag "Search" %>
</p>
<%end%>

Add the method in your controller to make a search:

def results
   attdstaus = params[:attdstaus]
   attddate= params[:attddate]
    @results = #implement your search here
  end

And send the search result back as 'results.html.eb' page:

<%if @results.empty?%>
  <h4>No results found</h4>
<%else%>
  <h4>Listing found students</h4>

  <table>
<tr>
      <th>Student name</th>
</tr
<% for student in @results %>
   <td><%=student.name %></td>
<%end>
</table>
<%end%>

Re: How to insert multiple records at a time in database in rails3

Hai Javix..............

    Thanks for ur reply......... But in ur answer where is insertion?

My requirement is after getting list of students then those students attendence have to insert into attendence table. Did you got it na?

I did upto getting students list then from here I have to give attendence to them. How?

Re: How to insert multiple records at a time in database in rails3

First of all, avoid naming your variables 'à la Java' as CamelCase, in Ruby we use underscored version.
As you don't respect Rails convention for naming DB table fields:

In students table i have stdname classname stdID fields (the preferred one is id)
In attendence table i have attdID stdID AttdDate fields.

you should define it in you r corresponding models. See [url!http://guides.rubyonrails.org/association_basics.html]Rails Guides[/url for more details].

By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix _id added. The :foreign_key option lets you set the name of the foreign key directly:

In your case, where the foreign key doesn't match the conventional one you should define it as follows:

class Attendence< ActiveRecord::Base
    set_primary_key :attd_id
  belongs_to :student,  :foreign_key => "std_id"
end

class Student < ActiveRecord::Base
  set_primary_key :std_id
  has_many :attendences
end

Last edited by Javix (2011-10-25 04:21:25)

Re: How to insert multiple records at a time in database in rails3

Yeah ur right Javix.........

              I too got like that only std_id but to explain my problem i used those terms like variables. Can u tell me by doing this then what to do to insert multiple records in database?

Re: How to insert multiple records at a time in database in rails3

I think the more suitable solution would be to use nested forms as it is explained by Ryan Bates at Rails casts. See episode 196 & 197. May be it will suit your needs.
The second point, I think you need to rethink you model relations or views. You are trying to display attendencies values for students, but ONE student can have ZERO or MANY attendencies, and in this case you will have something like this in the view (anyway it is smth you'd like to have to make un update later):

Student name | Att status | AttDAte
 John               | status 1   | 31-12-2010
 John               | status 2   | 01-01-2010
 Bill                  | status 12 |02-01-2010
...

You see ? Normally you should display attendencies for ONE student, but it is up to you to design; Just think about that. You can find more details on AR relations on Rails Guides

Last edited by Javix (2011-10-27 03:33:46)