0

I have a model that I use with active record in a ruby script below. Things like level_type, name, parent.id, and pipcode get assigned to the attributes of the model for entry into the levels table. My question is, does this only work for new records? you see, the name, parent.id, and pipcode get changed often. I dont want it to create a new record everytime these values change. I would like it to update existing records with the new values coming in from pipcode, parent.id etc..

My record creation code:

          new_region = Level.new(
            :level => level_type,
            :name => name,
            :parent_id => parent.id,
            :make_code => pipcode
          )
ian
  • 12,003
  • 9
  • 51
  • 107
Doublespeed
  • 1,153
  • 4
  • 16
  • 29

1 Answers1

1

Level.new(...) is usually used to create a new row when you call new_region.save()

If you want to update, first you need to find the row you want to update by calling something like:

# if patient_id is the primary key
existing_region = Level.find(patient_id)

At this point, you can update the existing_region's parameters and call .save() to update the database.

existing_region.level = level_type
existing_region.name = name
...
existing_region.save()
Chong Yu
  • 470
  • 3
  • 8