如何验证组(集合)的元素在Rails中每个父对象是唯一的



我的应用程序调用CoursesProjects以及GroupsUsers.。我需要确保users在每个project中出现的次数不会超过一次,我认为我可以通过Rails验证来做到这一点,但我所得到的似乎不起作用。有人能帮我吗?

下面给了我:

管理员中的NoMethodError:GroupsController#create
未定义的方法`text?'对于零:NilClass

class Group < ActiveRecord::Base
  attr_accessible :name, :project_id
  #has_and_belongs_to_many fields
  attr_accessible :user_ids
  has_and_belongs_to_many :users
  belongs_to :project, :inverse_of => :groups
  validates :name, :project_id, :presence => true
  validates :user_ids, :uniqueness => { :scope => :project_id,
                                        :message => "Users can only be in one group per project." }
end

ActiveAdmin组对象:

ActiveAdmin.register Group do
  form do |f|
    f.inputs do
      f.input :name
      f.input :users, :as => :check_boxes
      f.input :project
    end
    f.buttons
  end
end

我会尝试

Course
  has_many :projects
Project
  has_many :groups
  has_many :users, :through => groups
  validates uniqueness_of :user
User
  has_many groups 
  has_many projects, :through => :groups
Group
  belongs_to :project
  belongs_to :group
# I stay away from has_and_belongs_to_many, not flexible later.

相关内容

  • 没有找到相关文章

最新更新