如何根据两个属性删除重复项?

时间:2015-05-14 21:21:09

标签: ruby-on-rails postgresql activerecord duplicates

我的模型很简单:

create_table "stack_items", force: true do |t|
  t.integer  "stack_id"
  t.integer  "service_id"
  t.text     "description"
end

我需要删除具有相同StackItemstack_id的重复service_id条记录。但是,如果其中一个欺骗在description字段中有任何内容,我必须保留该字段,并删除其他副本。

 StackItem.group(:stack_id, :service_id).order("count_id desc").where("COUNT(*) > 1")

到目前为止,我试图抓住重复项,但它说我不能算在where语句中。

  

ActiveRecord :: StatementInvalid:PG :: GroupingError:ERROR:WHERE中不允许使用聚合函数

如何使用Rails 4和ActiveRecord实现这一目标?我的数据库是Postgresql。

0 个答案:

没有答案