Methodology for a Rails app

Posted by Aaron Vegh on Stack Overflow See other posts from Stack Overflow or by Aaron Vegh
Published on 2009-07-18T13:28:00Z Indexed on 2011/06/23 0:22 UTC
Read the original article Hit count: 231

Filed under:

I'm undertaking a rather large conversion from a legacy database-driven Windows app to a Rails app. Because of the large number of forms and database tables involved, I want to make sure I've got the right methodology before getting too far.

My chief concern is minimizing the amount of code I have to write. There are many models that interact together, and I want to make sure I'm using them correctly. Here's a simplified set of models:

class Patient < ActiveRecord::Base has_many :PatientAddresses has_many :PatientFileStatuses end

class PatientAddress < ActiveRecord::Base belongs_to :Patient end

class PatientFileStatus < ActiveRecord::Base belongs_to :Patient end

The controller determines if there's a Patient selected; everything else is based on that.

In the view, I will be needing data from each of these models. But it seems like I have to write an instance variable in my controller for every attribute that I want to use. So I start writing code like this:

@patient = Patient.find(session[:patient]) @patient_addresses = @patient.PatientAddresses @patient_file_statuses = @patient.PatientFileStatuses

@enrollment_received_when = @patient_file_statuses[0].EnrollmentReceivedWhen @consent_received = @patient_file_statuses[0].ConsentReceived @consent_received_when = @patient_file_statuses[0].ConsentReceivedWhen

The first three lines grab the Patient model and its relations. The next three lines are examples of my providing values to the view from one of those relations.

The view has a combination of text fields and select fields to show the data above. For example:

<%= select("patientfilestatus", "ConsentReceived", {"val1"=>"val1", "val2"=>"val2", "Written"=>"Written"}, :include_blank=>true )%> <%= calendar_date_select_tag "patient_file_statuses[EnrollmentReceivedWhen]", @enrollment_complete_when, :popup=>:force %>

(BTW, the select tag isn't really working; I think I have to use collection_select?)

My questions are:

  1. Do I have to manually declare the value of every instance variable in the controller, or can/should I do it within the view?
  2. What is the proper technique for displaying a select tag for data that's not the primary model?
  3. When I go to save changes to this form, will I have to manually pick out the attributes for each model and save them individually? Or is there a way to name the fields such that ActiveRecord does the right thing?

Thanks in advance, Aaron.

© Stack Overflow or respective owner

Related posts about ruby-on-rails