Rails,模型有三个其他相同模型的集合

时间:2015-03-16 07:37:47

标签: ruby-on-rails ruby collections associations mongoid

让我们说我的模型名为ServiceClient。客户已添加Services添加到书签,Services Client发出请求,Services Client已拨打电话。

这意味着我的Client有三个Services的收藏品。是否可以有多个存储相同模型的集合?当我使用SQl或MongoID时会有区别吗?

1 个答案:

答案 0 :(得分:2)

好的,如果我能正确理解你的问题。您有两个模型ServiceClient。 要求Client has Services that added to bookmark, Services that Client made request, Services that Client made call

我们假设我在Service(bookmarked, called, requested, client_id)模型上创建了三个属性,以确定服务是否已加书签或被调用或请求,当然它还有客户端ID。

class Client < ActiveRecord::Base
   has_many :bookmarked_services, -> {where bookmarked: true }, :class_name => "Service"
   has_many :requested_services, -> {where requested: true }, :class_name => "Service"
   has_many :called_services, -> {where called: true }, :class_name => "Service"
end

我认为以上解决方案应该可以解决您的问题。