【问题标题】:Rails, model that has three collections of other same modelsRails,具有三个其他相同模型集合的模型
【发布时间】:2015-03-16 07:37:46
【问题描述】:
假设我的模型名为 Service 和 Client。客户有 Services 添加到书签,Services 表示 Client 提出请求,Services 表示 Client 拨打电话。
这意味着我的Client 有三个Services 集合。是否可以有多个存储相同模型的集合?当我使用 SQl 或 MongoID 时会有差异吗?
【问题讨论】:
标签:
ruby-on-rails
ruby
collections
associations
mongoid
【解决方案1】:
好的,如果我正确理解了您的问题。您有两个模型 Service 和 Client。
以及要求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
我认为上述解决方案应该可以解决您的问题。