Ruby on Rails update element in database via Migrate file -
i need update several tuple's name attributes in database. locally can update data via gui -- pgadmin.
but i'm new rails , not sure how update developer database.
a coworker briefly suggested editing db/migrate file accomplished. can method?
or better done via command line?
i'm using postgres.
class updateactionableitemname < activerecord::migration class insightreportmenuitem < activerecord::base self.table_name = 'actionable_items' attr_accessible :name end def #find rows updated prescriber_activity_request = insightreportmenuitem.where(name: "prescriber activity 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 rows 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 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
yes, can via rails console, however, migration file better way so , made same effect developers' machines when pull latest code, error run migration file. this, same change machines.
note don't edit migration file once pushed repo. create new migration file , add code manipulate database records required.
update:
i not sure if understand your comment, however, if want know way add code migration file update row, same on rails console. example if have table users
, need update name
column id
60. in case have model class:
user = user.find(60) user.update_attribute(name: "new name") if user
in case don't have model class user
, can directly update row using raw sql as:
query = "update users set name = 'new name' id = 60" activerecord::base.connection.execute(sql)
Comments
Post a Comment