I have the following requirement: model is called Medical; it has a Doctor Type and a Treatment Type; there can be multiple Doctor Types and Treatment Types chosen for a Medical model.
I am trying to avoid creating models for Doctor Type and Treatment Type. They are basically a collection that the application provides its users. I am trying to store them on the Medical model as hstore fields.
I did the following...
Added a migration for hstore:
class AddHstore < ActiveRecord::Migration
def up
enable_extension :hstore
end
def down
disable_extension :hstore
end
end
Added migration to add hstore field:
class AddDataToMedical < ActiveRecord::Migration
def change
add_column :medical, :data, :hstore
end
end
Added the following to the model:
class Medical < ActiveRecord::Base
DOCTOR_TYPE = ['MD', 'Specialist', 'DO', 'Other']
TREATMENT_TYPE = ['PT', 'Chiro', 'Meds', 'Other']
store_accessor :data, :doctor_type, :treatment_type
...
Added the following into strong_params:
# Never trust parameters from the scary internet, only allow the white list through.
def medical_params
params.require(:medical).permit(:total_med_bills, :subrogated_amount, :injuries_within_three_days, :length_of_treatment,
:length_of_treatment_unit, :injury_summary, :medical_summary,
:earnings_lost, :treatment_gap, :injections, :hospitalization, :hospital_stay_length,
:hospital_stay_length_unit, :data => [:doctor_type, :treatment_type], :injuries_attributes => [:injury_type, :region, :code, :dominant_side, :joint_fracture,
:displaced_fracture, :disfigurement, :impairment, :permanence, :disabled,
:disabled_percent, :surgery, :surgery_count, :surgery_type, :casted_fracture,
:stitches, :future_surgery, :future_medicals, :id])
end
then added the new fields to the form:
<%= simple_form_for([@case, @medical]) do |f| %>
<%= f.input :total_med_bills %>
<%= f.input :length_of_treatment %>
<%= f.simple_fields_for :data do |d| %>
<%= d.input :doctor_type, as: :check_boxes, collection: Medical::DOCTOR_TYPE %>
<%= d.input :treatment_type, as: :check_boxes, collection: Medical::TREATMENT_TYPE %>
<% end %>
<%= f.input :injury_summary %>
...
Question is why is the data parameter empty when I do a puts params[:data] in my Update method of medicals_controller (i am only using this field in the edit)? Also why am I getting the following in my console:
Unpermitted parameters: doctor_type, treatment_type
I see that the data hash is NOT empty when I look at my console:
..."length_of_treatment"=>"", "data"=>{"doctor_type"=>["MD", "Specialist", ""], "treatment_type"=>["Meds", "Other", ""]}, "injury_summary"=>"",...
Since strong parameters doesn't allow scalar values in the strong parameters...I ended up doing the following...
Strong parameters:
... :data => [:doctor_type => [], :treatment_type => []] ...
doctor_type
and treatment_type
keys can have multiple values.