Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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 Rails:基于列销毁除最新之外的所有内容_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails:基于列销毁除最新之外的所有内容

Ruby on rails Rails:基于列销毁除最新之外的所有内容,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个授权模型,里面有过期的副本。我想根据授权中的:provider列删除除最新之外的所有内容,留下最新的“linkedin”授权、最新的“facebook”等 我将如何编写该方法?逻辑上是这样的,但它给出了这个错误(TypeError:无法将授权转换为数组(authorization\to\u-class)): 也许这个: last_record = Authorisation.where(provider: 'facebook').last Authorisation.where('c

我有一个
授权
模型,里面有过期的副本。我想根据
授权
中的
:provider
列删除除最新之外的所有内容,留下最新的
“linkedin”
授权、最新的
“facebook”

我将如何编写该方法?逻辑上是这样的,但它给出了这个错误(
TypeError:无法将授权转换为数组(authorization\to\u-class)
):

也许这个:

 last_record = Authorisation.where(provider: 'facebook').last
 Authorisation.where('created_at < ?', last_record.created_at).delete_all
但我认为它只适用于Rails4

更新

这样更好:

Authorisation.where('id != ?', Authorisation.last.id).delete_all
 Authorisation.where.not(id: Authorisation.last.id).delete_all
Authorisation.where('id != ?', Authorisation.last.id).delete_all