Kendall Weihe
Kendall Weihe

Reputation: 193

Ruby on Rails update element in database via Migrate file

I need to update several tuple's name attributes in the database. Locally I can just update the data via a GUI -- pgAdmin.

But I'm new to rails and am not sure how to actually update the developer database.

A coworker briefly suggested editing a db/migrate file to get this accomplished. Can I do this with some method?

Or is something like this better done via the command line?

I'm using postgres.

class UpdateActionableItemName < ActiveRecord::Migration

  class InsightReportMenuItem < ActiveRecord::Base
    self.table_name = 'actionable_items'
    attr_accessible :name
  end

  def up
    #find all the rows to be updated
    prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity With Patient")
    dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity")
    patient_history_request = InsightReportMenuItem.where(name: "Patient Request Activity")
    #update row attributes
    prescriber_activity_request.update_attribute(name: "Prescriber Activity Request") if prescriber_activity_request
    dispenser_activity_request.update_attribute(name: "Dispenser Activity Request") if dispenser_activity_request
    patient_history_request.update_attribute(name: "Patient History Request") if patient_history_request
    #save updates
    prescriber_activity_request.save!
    dispenser_activity_request.save!
    patient_history_request.save!
  end

  def down
    #find all the rows to be updated
    prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity Request")
    dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity Request")
    patient_history_request = InsightReportMenuItem.where(name: "Patient History Request")
    #update row attributes
    prescriber_activity_request.update_attribute(name: "Prescriber Activity With Request") if prescriber_activity_request
    dispenser_activity_request.update_attribute(name: "Dispenser Activity") if dispenser_activity_request
    patient_history_request.update_attribute(name: "Patient Request Activity") if patient_history_request
    #save updates
    prescriber_activity_request.save!
    dispenser_activity_request.save!
    patient_history_request.save!
  end
end

Upvotes: 0

Views: 1007

Answers (1)

RAJ
RAJ

Reputation: 9747

Yes, you can do it via Rails Console, however, migration file is better way to so so and to made same effect to all the developers' machines when they will pull latest code, they will get error to run migration file. With this, they will get the same change to their machines.

Note that DON'T edit migration file once pushed to your repo. Create new migration file and add code to manipulate your database records as required.

Update:

I am not sure if I understand your your comment, however, if you want to know the way to add the code into migration file to update the row, it same as we do on rails console. For example if you have table users and you need to update name column for id 60. In case you have the model class:

user = User.find(60)
user.update_attribute(name: "New Name") if user

In case you don't have model class User, you can directly update the row by using raw SQL as:

query = "UPDATE users set name = 'New Name' WHERE id = 60"
ActiveRecord::Base.connection.execute(sql)

Upvotes: 1

Related Questions