Saving survey data from the notesonrails application is not just a matter of adding something new — it also involves destroying something old. Today’s work doesn’t get us a completely functional system, but it migrates the system from a “notes” to a “survey response” method of saving data, and lays the groundwork for more tomorrow…
The old notes model is built on the idea that all instrument data involves the following structure
ID
StudentId
NoteText
NotesFieldId
NotesRecordID
However, now that notes are just detailed questions, a better format would be
Id
StudentId
QuestionListID
QuestionId
Field_Identifier
Record_Identifier
SurveyText
On one level this replaces record/field with questionlist/question as the primary identifier of where data comes from. Field and record can now be used by other purposes as well.
First, generate a model for the new survey_responses
ruby script/generate model survey_response
This creates 027_create_survey_responses.db, which should be altered to read (recall the ID is created manually):
class CreateSurveyResponses < ActiveRecord::Migration
def self.up
create_table :survey_responses do |t|
t.column :student_id, :integer
t.column :question_list_id, :integer
t.column :question_id, :integer
t.column :field_identifier, :integer
t.column :record_identifier, :integer
t.column :survey_text, :string
end
end
def self.down
drop_table :survey_responses
end
end
And next, delete the old notes table
ruby script/generate migration dump_notes
The new file that creates, 028_dump_notes.rb, should read (now that we entered the current description of the notes table in case we want to undo this)
class DumpNotes < ActiveRecord::Migration
def self.up
drop_table :notes
end
def self.down
create_table :notes do |t|
t.column :student_id, :integer
t.column :notetext, :string
t.column :notes_field_id, :integer
t.column :notes_record_id, :integer
end
end
end
And then, rake db:migrate
Good. The next part is just relatively straightforward: update the students controller to actually save information.
…
And in the _notes_view.rhtml partial, you’ll need to add lines to identify the detailed data of the notes survey responses, as well as getting rid of the old and redundant ajax code from prevoius days:
<table border="1">
<tr>
<td> </td>
<% for notes_record in @notes_records %>
<td><%= h(notes_record.name) %></td>
<% end %>
</tr>
<% for notes_field in @notes_fields %>
<tr>
<td><%= notes_field.name %></td>
<% for notes_record in @notes_records %>
<td><%= text_area h(notes_field.name), h(notes_record.name), :cols => 20, :rows => 8 %></td>
<br /><%= hidden_field_tag ‘field_’ + question.id.to_s, notes_field.id %>
<br /><%= hidden_field_tag ‘record_’ + question.id.to_s, notes_record.id %>
<% end %>
</tr>
<% end %>
</table>
For its part, the relevent section of _question_types.rhtml changes hardly at all:
<% if question.type_id == QuestionType::NOTES_MATRIX or question.type_id == QuestionType::NOTES_LINEAR %>
<br />Before, we go father, question is <%= question.id %>
<% @notes_fields = NotesField.find_by_condition(@student.condition_id) %>
<% @notes_records = NotesRecord.find_by_condition(@student.condition_id) %>
<%= hidden_field_tag "question_" + question.id.to_s %>
<%= render :partial => "notes_view", :locals => {:question => question } %>
<% end %>
Likewise, modify students_controller.rb so it starts like this:
def run_experiment
@student = Student.find(session[:student])
unless @student.condition_id
@student.condition_id = params[:student][:condition_id]
@student.save
end
## Saving data goes here
@questions = params[:question]
if (@questions)
for question in @questions
question_number = question[0]
question_data = question[1]
question_field = params[‘field_’ + question_number]
question_record = params[‘record_’ + question_number]
@survey_response = SurveyResponse.new(
:student_id => @student.id,
:question_list_id => @student.current_ordering,
:question_id => question_number,
:field_identifier => question_field,
:record_identifier => question_record,
:survey_text => question_data
)
@survey_response.save()
end
end
## stop saving data here
There’s still stuff to do. Complicated form data, such as checkboxes, are not saved correctly, and the notes are still not saved at all. But it’s a start