我对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
答案 0 :(得分:1)
尝试使用
has_many :products, -> { distinct }, through: :clients_features_products
<{1>}模型上的
请参阅http://guides.rubyonrails.org/association_basics.html#distinct
其他一些事情
Client
代替all_data
。有关详细信息,请参阅样式指南:https://github.com/styleguide/ruby allData
代替preload
;包括将作为预加载或类似的连接,你不知道哪个,所以我倾向于远离它;好文章:http://blog.arkency.com/2013/12/rails4-preloading/ 答案 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视图。