I have the following migration file ...
... the goal is to change several name attributes in the database ...
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")
debugger
#update row attributes
prescriber_activity_request.update_attributes(:name, "Prescriber Activity Request") if prescriber_activity_request
dispenser_activity_request.update_attributes(:name, "Dispenser Activity Request") if dispenser_activity_request
patient_history_request.update_attributes(: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_attributes(:name, "Prescriber Activity With Request") if prescriber_activity_request
dispenser_activity_request.update_attributes(:name, "Dispenser Activity") if dispenser_activity_request
patient_history_request.update_attributes(:name, "Patient Request Activity") if patient_history_request
#save updates
prescriber_activity_request.save!
dispenser_activity_request.save!
patient_history_request.save!
end
end
... but when I run rake db:migration I get an error ...
... what's wrong here?
I've tried several different versions of syntax and I'm still getting the same bounce back error.
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `update_attributes' for #<ActiveRecord::Relation:0x0000010e2e4d20>/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/relation/delegation.rb:45:in `method_missing'
/Users/kweihe/pmpaware-webapp/db/migrate/20150706132233_update_actionable_item_name.rb:15:in `up'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:389:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:528:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `call'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/transactions.rb:208:in `transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `ddl_transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:719:in `block in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `each'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:570:in `up'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:551:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
UPDATE: SOLVED
added .first to the end of the query
prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity With Patient").first
dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity").first
patient_history_request = InsightReportMenuItem.where(name: "Patient Request Activity").first
update_attributes
should be called on a single instance.
Your variables, such as prescriber_activity_request
are relations, which are a wrapper around an SQL query. Try changing it to eg
prescriber_activity_request.update_all(:name, "Prescriber Activity With Request") if prescriber_activity_request.size > 0