Reputation: 975
How do I validate that a model has at least one associated model using nested attributes? This has been driving me crazy as I am sure that I am missing something simple. For example, I want to require that a List always has at least one Task.
class List < ActiveRecord::Base
has_many :tasks, :dependent => :destroy
accepts_nested_attributes_for :tasks, :allow_destroy => true
end
class Task < ActiveRecord::Base
belongs_to :list
end
I've tried many different options.
1- adding a validation to lists:
def validate
if self.tasks.length < 1
self.errors[:base] << "A list must have at least one task."
end
end
but this will still allow you to delete all the tasks of an existing list since when deleting tasks the validation of list happens before the tasks are destroyed.
2- checking to see if any tasks are not marked for destruction in a before_save callback
before_save :check_tasks
private
#look for any task which won't be deleted
def check_tasks
for t in self.tasks
return true if ! t.marked_for_destruction?
end
false
end
For some reason I can't get it to ever delete a task with anything that iterates over a list's tasks. The same is true if I do this check in def validate
instead of a callback
3- requiring the presence of tasks validates_presence_of :tasks
, but with this it won't ever delete any tasks
Upvotes: 4
Views: 5024
Reputation: 975
I ended up extending Magazine's save method to get around the problem. It worked like a charm.
def save
saved = false
ActiveRecord::Base.transaction do
saved = super
if self.conditions.size < 1
saved = false
errors[:base] << "A rule must have at least one condition."
raise ActiveRecord::Rollback
end
end
saved
end
Upvotes: 3
Reputation: 6225
You can check both conditions together in validation method:
validate :check_tasks
def check_tasks
if self.tasks.size < 1 || self.tasks.all?{|task| task.marked_for_destruction? }
errors.add_to_base("A list must have at least one task.")
end
end
Upvotes: 8