我的模型很简单:
create_table "stack_items", force: true do |t|
t.integer "stack_id"
t.integer "service_id"
t.text "description"
end
我需要删除具有相同StackItem
和stack_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。