导轨:如何限制的has_many关联的项目数(母公司) [英] Rails: How to limit number of items in has_many association (from Parent)

查看:119
本文介绍了导轨:如何限制的has_many关联的项目数(母公司)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制的关联的项目数。我想确保用户不会有更多的超过X的东西。这个问题被要求前,该解决方案具有逻辑孩子

I would like to limit the number of items in an association. I want to ensure the User doesn't have more than X Things. This question was asked before and the solution had the logic in the child:

class User < ActiveRecord::Base
  has_many :things, :dependent => :destroy
end

class Thing <ActiveRecord::Base
  belongs_to :user
  validate :thing_count_within_limit, :on => :create

  def thing_count_within_limit
    if self.user.things(:reload).count >= 5
      errors.add(:base, "Exceeded thing limit")
    end
  end
end

硬codeD5是一个问题。基于父我的极限了变化。物联网收集知道相对于用户的限制。在我们的例子中,一个经理可以为每个用户调整(物联网)的限制,因此用户必须限制物联网的集合。我们的可以的有thing_count_within_limit要求从它的用户的限制:

The hard coded "5" is an issue. My limit changes based on the parent. The collection of Things knows its limit relative to a User. In our case, a Manager may adjust the limit (of Things) for each User, so the User must limit its collection of Things. We could have thing_count_within_limit request the limit from its user:

if self.user.things(:reload).count >= self.user.thing_limit

不过,这是一个很大的用户从事情反省。多次调用用户,尤其是那个(:重装)。正红旗我

But, that's a lot of user introspection from Thing. Multiple calls to user and, especially, that (:reload) are red flags to me.

我觉得的has_many:事情,:before_add =&GT; :limit_things 的工作,但我们必须抛出一个异常的停止链。这迫使我更新things_controller来处理异常,而不是对轨道约定如果有效?,如果保存

I thought has_many :things, :before_add => :limit_things would work, but we must raise an exception to stop the chain. That forces me to update the things_controller to handle exceptions instead of the rails convention of if valid? or if save.

class User
  has_many :things, :before_add => limit_things

  private
  def limit_things
    if things.size >= thing_limit
      fail "Limited to #{thing_limit} things")
    end
  end
end

这是Rails的。如果我有努力过,我可能做错了什么。

要做到这一点,我必须更新父模型,对孩子的控制,我不能按照约定?我失去了一些东西?我是否滥用的has_many,:before_add ?我找了一个例子使用:before_add,但找不到任何

This is Rails. If I have to work this hard, I'm probably doing something wrong.

To do this, I have to update the parent model, the child's controller, AND I can't follow convention? Am I missing something? Am I misusing has_many, :before_add? I looked for an example using :before_add, but couldn't find any.

我想到了移动验证到用户,但是这只是发生在用户保存/更新。我不明白的方式来使用它来停止添加东西的。

I thought about moving the validation to User, but that only occurs on User save/update. I don't see a way to use it to stop the addition of a Thing.

我preFER了对Rails 3的解决方案(如果该事项对这个问题)。

I prefer a solution for Rails 3 (if that matters for this problem).

推荐答案

所以,如果你想要一个不同的限制,每个用户可以添加things_limit:整数到用户,做

So if you want a different limit for each user you can add things_limit:integer into User and do

class User
  has_many :things
  validates_each :things do |user, attr, value|
   user.errors.add attr, "too much things for user" if user.things.size > user.things_limit
  end
end

class Thing
  belongs_to :user
  validates_associated :user, :message => "You have already too much things."
end

本code,你不能更新user.things_limit为数字比所有他已经得到的东西时,当然它限制他user.things_limit创造的东西的用户。

with this code you can't update the user.things_limit to a number lower than all the things he already got, and of course it restrict the user to create things by his user.things_limit.

应用例如轨道4:

<一个href="https://github.com/senayar/user_things_limit">https://github.com/senayar/user_things_limit

这篇关于导轨:如何限制的has_many关联的项目数(母公司)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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