Ich habe zwei Modelle: Project
und ProjectDiscipline
:Rails 3: Vorhandensein von mindestens zu validieren hat man viele durch Vereinigung Artikel
class Project < ActiveRecord::Base
has_many :project_disciplinizations, :dependent => :destroy
has_many :project_disciplines, through: :project_disciplinizations
attr_accessible :project_discipline_ids
attr_accessible :project_disciplines_attributes
accepts_nested_attributes_for :project_disciplines, :reject_if => proc { |attributes| attributes['name'].blank? }
end
class ProjectDiscipline < ActiveRecord::Base
attr_accessible :name
has_many :project_disciplinizations, :dependent => :destroy
has_many :projects, through: :project_disciplinizations
end
class ProjectDisciplinization < ActiveRecord::Base
attr_accessible :project_discipline_id, :project_id
belongs_to :project_discipline
belongs_to :project
end
Auf der neuen bearbeiten/Project
Form habe ich eine Liste von Disziplinen und einem Scheck für jeden von ihnen Box, so dass Benutzer Disziplinen auswählen können:
<div class="control-group">
<label class="control-label">Check disciplines that apply</label>
<div class="controls">
<%= f.collection_check_boxes(:project_discipline_ids, ProjectDiscipline.order('name'), :id, :name, {}, {:class => 'checkbox'}) {|input| input.label(:class => 'checkbox') { input.check_box + input.text }} %>
<p class="help-block">You must choose at least one discipline.</p>
</div>
</div>
ich, dass zumindest eine eine Validierung erfordern hinzufügen möchten Disziplin überprüft wird. Ich habe es versucht, aber ich habe es noch nicht herausgefunden. Wie kann ich diese Validierung hinzufügen?
Können Sie meine Antwort überprüfen? – asiniy
Wenn Sie die Disziplin später zerstören, kann es passieren, dass ein Projekt keine Disziplinen mehr hat und somit in einem ungültigen Zustand ist. – wacha
'.lepty?' Wäre hier besser geeignet, leere Abfragen gibt den Datensatz aus der DB zurück, leer 'SELECT 1 AS one FROM 'Projekt_disziplinen' WHERE UND 'project_disciplines' .'id' = 1 LIMIT 1', was leicht ist effizienter. –