Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 什么';删除模型对象数组的最快方法是什么?_Ruby On Rails_Arrays_Ruby On Rails 5_Destroy - Fatal编程技术网

Ruby on rails 什么';删除模型对象数组的最快方法是什么?

Ruby on rails 什么';删除模型对象数组的最快方法是什么?,ruby-on-rails,arrays,ruby-on-rails-5,destroy,Ruby On Rails,Arrays,Ruby On Rails 5,Destroy,我使用的是Rails 5。我想从表中删除一组记录。我有 currencies = CryptoCurrency.order('latest_market_cap_in_usd desc').limit(num_currencies) current_index_currencies = CryptoIndexCurrency.all.pluck(:crypto_currency_id) currencies_to_remove = current_index_currencies - curr

我使用的是Rails 5。我想从表中删除一组记录。我有

currencies = CryptoCurrency.order('latest_market_cap_in_usd desc').limit(num_currencies)
current_index_currencies = CryptoIndexCurrency.all.pluck(:crypto_currency_id)

currencies_to_remove = current_index_currencies - currencies
...
currencies_to_remove.each do |currency|
  currency.destroy
end
其中“Currency_to_remove”应该包含我要删除的所有模型的数组。但在遍历列表时,我得到以下错误

2.4.0 :005 > svc.create_index
   (0.4ms)  SELECT "crypto_index_currencies"."crypto_currency_id" FROM "crypto_index_currencies"
  CryptoCurrency Load (1.5ms)  SELECT  "crypto_currencies".* FROM "crypto_currencies" ORDER BY latest_market_cap_in_usd desc LIMIT $1  [["LIMIT", 12]]
NoMethodError: undefined method `destroy' for 1020:Integer
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/whenever-0.9.7/lib/whenever/numeric.rb:10:in `method_missing'
    from /Users/davea/Documents/workspace/cindex/app/services/crypto_index_service.rb:17:in `block in create_index'
    from /Users/davea/Documents/workspace/cindex/app/services/crypto_index_service.rb:16:in `each'
    from /Users/davea/Documents/workspace/cindex/app/services/crypto_index_service.rb:16:in `create_index'
    from (irb):5
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.4/lib/rails/commands/console.rb:65:in `start'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.4/lib/rails/commands/console_helper.rb:9:in `start'

这是因为
currences\u to\u remove
包含要删除的模型的索引,而不是模型的索引。因此,
destroy
方法未定义

删除它们的最快方法是模型类上的
delete
方法:

CryptoCurrency.delete(currencies_to_remove)
但是如果需要调用destroy上的回调,则可能需要
destroy
方法:

CryptoCurrency.destroy(currencies_to_remove)

请注意,
destroy
方法将引发
RecordNotFound
异常,如果指定数组中的任何记录未找到

,这是因为
currences\u to\u remove
包含要删除的模型的索引,而不是模型的索引。因此,
destroy
方法未定义

删除它们的最快方法是模型类上的
delete
方法:

CryptoCurrency.delete(currencies_to_remove)
但是如果需要调用destroy上的回调,则可能需要
destroy
方法:

CryptoCurrency.destroy(currencies_to_remove)
请注意,如果在指定数组中找不到任何记录,则
destroy
方法将引发
RecordNotFound
异常