Search code examples
ruby-on-railsrubydam

Ruby adding to db


I have problem because when I try to add data to my database using form I get error unknown_attribute id_lekarza (the same with id_pacjenta and id_poradni) and I don't know why, please help Form:

<div id="container">

<%= form_for(@refferal) do |f| %>
  <div class="field">
    <%= f.label :rodzaj_badania %><br />
    <%= f.text_field :rodzaj_badania, placeholder: "Podaj jakie badanie ma byc wykonane", size: 35 %>
  </div>
  <div class="field">
    <%= f.label :ilosc_badan %><br />
    <%= f.number_field :ilosc_badan, placeholder: "Wpisz ilosc badan"  %>
  </div>
  <div class="field">
    <%= f.label :opis %><br />
    <%= f.text_field :opis, placeholder: "Podaj krótki opis dla pracownika rejestracji/laboratorium", size: 50%>
  </div>
  <div class="field">
    <%= f.label :data %><br />
    <%= f.datetime_select :data, placeholder: "Podaj termin badania"  %>
  </div>
  <div class="field">
    <%= f.hidden_field :id_lekarza, :value => Doctor.find(session[:current_doctor_id]).id %>
  </div>
    <div class="field">
    <%= f.hidden_field :id_pacjenta, :value => Patient.find(session[:current_patient_id]).id %>
  </div>
    <div class="field">
    <%= f.hidden_field :id_poradni, :value => Clinic.find(session[:current_clinic_id]).id %>
  </div>
  <div class="actions">
    <%= f.submit "Utwórz skierowanie" %>
  </div>
<% end %>
</div>

Model:

class Refferal < ActiveRecord::Base
  attr_accessible :data, :id_lekarza, :id_pacjenta, :id_poradni, :id_pracownika, :ilosc_badan, :opis, :rodzaj_badania
    belongs_to :patient
    belongs_to :employee
    belongs_to :clinic
    belongs_to :doctor

    has_many :laboratoria

end

Schema:

  create_table "refferals", force: true do |t|
    t.string   "rodzaj_badania"
    t.integer  "ilosc_badan"
    t.string   "opis"
    t.datetime "data"
    t.integer "id_lekarza"
    t.integer "id_pacjenta"
    t.integer "id_pracownika"
    t.integer "id_poradni"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Solution

  • It looks like your belongs_to associations might be your problem. Rails is really opinionated about what it expects you to name things. If your model belongs to a doctor (which is the case with Refferal model), Rails is expecting the attribute to be called doctor_id.

    Try this:

    Change the attribute names from :id_lekarza to :lekarza_id, and so forth for the others. Then change your model associations to specify the actual class those attributes belong to:

    class Refferal < ActiveRecord::Base
      attr_accessible :data, :id_lekarza, :id_pacjenta, :id_poradni, :id_pracownika, :ilosc_badan, :opis, :rodzaj_badania  
    
      belongs_to :pacjenta, class_name: 'Patient'
      belongs_to :employee
      belongs_to :poradni, class_name: 'Clinic'
      belongs_to :lekarza, class_name: 'Doctor'
      has_many :laboratoria
    end
    

    Also, you'll probably want to change your has_many association to the plural: has_many :laboratorias

    Good luck!