限制has_many与mongoid关联中的对象数 [英] Limit number of objects in has_many association with mongoid

查看:49
本文介绍了限制has_many与mongoid关联中的对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制帖子和图片之间has_many关联中关联对象的数量:

I want to limit the number of associated objects in a has_many association between a post and pictures :

在活动记录中,我可以做类似的事情

In active record i can do something like

class post < < ActiveRecord::Base
  has_many :pictures, :limit => 2
end

但是mongoid提出了一个带有限制的异常:

But mongoid raise an exception with limit:

为关系:picture提供的无效选项:limit.有效选项 是:as,自动保存,从属,foreign_key,order,class_name,extend, inverse_class_name,inverse_of,名称,关系,验证. (Mongoid :: Errors :: InvalidOptions)

Invalid option :limit provided to relation :pictures. Valid options are: as, autosave, dependent, foreign_key, order, class_name, extend, inverse_class_name, inverse_of, name, relation, validate. (Mongoid::Errors::InvalidOptions)

是否存在另一种实现relize:limit的实现?

Is there an alternative implementation to relize :limit?

推荐答案

实际上并没有限制关联中的对象数量,它只是限制了查询关系时返回的结果数量.您可能想要这样的答案限制has_many关联中的对象数量-将向您展示如何在验证过程中检查关联对象的数量.

using has_many :limit doesn't actually restrict the number of objects in the association -- it just limits the number of results returned when querying the relation. You probably want something like this answer Limit number of objects in has_many association -- which will show you how to check the number of assocated objects during validation.

这篇关于限制has_many与mongoid关联中的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆