Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 way working with multiple if statements and form create/new
    primarykey
    data
    text
    <p>I have a problem to approach and not sure what the most appropriate method will be to make this work. Here the background to begin: </p> <p>There are two models I am working with Procedures and Appointments. The Appointments model belongs_to the Procedures model and Procedures model has_many Appointments.</p> <p>Now on the procedures model there are two key points to focus on, rather, two key columns.</p> <p><code>attr_accessible :visits, :occurence</code></p> <p>visits is the specific number of times to schedule the Appointment(s). occurence is the frequency of the visits. An example would be <code>visits: "5", occurence: "weekly"</code></p> <p>So when I am submitting my form I would like to write a method that looks at both <code>visits: "x"</code> and <code>occurence: ["weekly", "biweekly", "monthly"]</code> to then create a if or a switch -- php does switch still looking into ruby version -- but I suspect there is an elegant way to write this up. </p> <p>My current create method looks like this:</p> <pre class="lang-rb prettyprint-override"><code>def create @appointment = Appointment.new(params[:appointment]) set_variables if @appointment.save flash[:success] = "Appointment scheduled!" redirect_to patient_path(@current_patient) else redirect_to patient_path(@current_patient) flash[:error] = "Appointment Date and Time cannot be blank, please try again." end end </code></pre> <p>What would be the best way to tackle a) identifying <code>occurence: ["weekly", "biweekly", "monthly"]</code> and then processing <code>visits: "x"</code> based on something similar to:</p> <pre class="lang-rb prettyprint-override"><code>if @appointment.occurence == "weekly" (x-1).times do |n| submit same params but change within params appointment_date: = ((@appointment.appointment_date) + (n+1).week.to_formatted_s(:db) @appointment.save end end </code></pre> <p>...and so on and so forth using <code>(n+1).month</code> for monthly occurrence <code>(n+2).day</code> and for bi-weekly occurrence(s).</p> <p>Thank you in advance, hope this clarifies things. Just one item to note, do I need to store in database <code>visits:</code> and <code>occurence:</code>, I suspect not but would like to be certain they are used when hitting the models_controller create function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload