Rails 未定义的方法 update_attributes

Rails undefined method update_attributes

我有以下迁移文件...

...目标是更改数据库中的几个名称属性...

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

...但是当我 运行 rake db:migration 我得到一个错误...

...这是怎么回事?

我尝试了几种不同版本的语法,但仍然遇到相同的退回错误。

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)>'

更新:已解决

在查询末尾添加了 .first

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 应在单个实例上调用。

您的变量,例如 prescriber_activity_request 是关系,它们是 SQL 查询的包装器。尝试将其更改为 eg

    prescriber_activity_request.update_all(:name, "Prescriber Activity With Request") if prescriber_activity_request.size > 0