Rails未定义方法update_attributes

时间:2015-07-06 15:58:32

标签: ruby-on-rails methods migration undefined 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)>'

更新:已解决

首先添加。到查询结尾

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

1 个答案:

答案 0 :(得分:2)

应该在单个实例上调用

update_attributes

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

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