Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Rails 中,我有两个模型,组和用户。我该怎么做,用户只有一个组,而 goupr 最多有 4 个用户? 我在 users.rbbelongs_to :group和 groups.rb中试过这个has_many :users
belongs_to :group
has_many :users
谢谢
例如使用自定义验证:
class Group has_many :users validate :limit_users private def limit_users errors.add('Only 4 users allowed') if users.size > 4 end end class User belongs_to :group end