Rails 3.1限制用户创建的对象

我想限制用户可以创建的模型对象的数量。 我已经尝试过以下但是没有用。 我理解rails 3.1中发生了一些变化,现在还不确定如何实现。

class User  5, :dependent => :destroy # This doesn't work end class Things <ActiveRecord::Base belongs_to :user end 

尝试这样的事情:

 class User < ActiveRecord::Base has_many :things end class Things  :create def thing_count_within_limit if self.user.things(:reload).count >= 5 errors.add(:base, "Exceeded thing limit") end end end 

编辑 :更新为Rails 3

它在Rails 3.2.1上不起作用。 计数总是等于0 。 我用self.user.things.size替换它,现在它可以工作了。