在查询之后和返回数据之前删除重复记录

时间:2015-10-13 18:02:10

标签: ruby-on-rails ruby rails-activerecord

我对Rails相当陌生并且熟悉发电机,但这是我第一次尝试创建自定义路线。

我有一个星型模式,其中包含三个表客户产品功能,每个表彼此之间具有n / n关系,所以我的"明星"联结表有三个外键, client_id product_id feature_id 。我创建了一个API,它将返回一个客户端列表,每个客户端都有一个嵌套的产品列表,每个产品都有一个嵌套的功能列表。这是构建并返回结果的方法:

def return_all
  allData = Client
                .includes(:products)
                .includes(:features)
                .to_json(:include => {:products => {:include => :features}})
  render json: allData
end

这个问题是每个产品都有多个功能,因此生成的JSON会返回此信息(为了简洁起见,我删除了除一个客户端和一些数据之外的所有数据):

{
"id": 13,
"name": "client_1",
"created_at": "2015-10-09T18:44:56.000Z",
"updated_at": "2015-10-09T18:44:56.000Z",
"products": [
    {
        "id": 1,
        "name": "product_1",
        "created_at": "2015-10-12T03:10:34.000Z",
        "updated_at": "2015-10-12T03:10:34.000Z",
        "features": [
            {
                "id": 9,
                "name": "email",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 10,
                "name": "sms",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 11,
                "name": "ivr",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 12,
                "name": "print",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            }
        ]
    },
    {
        "id": 1,
        "name": "product_1",
        "created_at": "2015-10-12T03:10:34.000Z",
        "updated_at": "2015-10-12T03:10:34.000Z",
        "features": [
            {
                "id": 9,
                "name": "email",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 10,
                "name": "sms",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 11,
                "name": "ivr",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            },
            {
                "id": 12,
                "name": "print",
                "created_at": "2015-10-12T03:10:55.000Z",
                "updated_at": "2015-10-12T03:10:55.000Z",
                "value": null,
                "group": "channel"
            }
        ]
    }
]
}

如何删除重复的产品列表?如果您需要有关我的问题的更多信息,请与我们联系。

由于

编辑:根据要求,这是我的模特:

class ClientsFeaturesProduct < ActiveRecord::Base
  belongs_to :client
  belongs_to :feature
  belongs_to :product
end

class Client < ActiveRecord::Base
  has_many :clients_features_products
  has_many :features, through: :clients_features_products
  has_many :products, through: :clients_features_products
end

class Product < ActiveRecord::Base
  has_many :clients_features_products
  has_many :clients, through: :clients_features_products
  has_many :features, through: :clients_features_products
end

class Feature < ActiveRecord::Base
  has_many :clients_features_products
  has_many :clients, through: :clients_features_products
  has_many :products, through: :clients_features_products
end

2 个答案:

答案 0 :(得分:1)

尝试使用

has_many :products, -> { distinct }, through: :clients_features_products 
<{1>}模型

上的

请参阅http://guides.rubyonrails.org/association_basics.html#distinct

其他一些事情

答案 1 :(得分:0)

我会尝试这个,但这只是猜测。

def return_all
  allData = Client
                .joins(:products) # might need to use raw sql
                .joins(:features)
                .to_json(:include => {:products => {:include => :features}})
  render json: allData
end

或者,您可以使用Active Model Serializers显式定义json视图。