Tengo un error al hacer db:reset
Users has been created
rails aborted!
Mongoid::Errors::Validations:
message:
Validation of Task failed.
summary:
The following errors were found: Participating users is invalid
resolution:
Try persisting the document with valid data or remove the validations.
Dejo mis seeds
['alcides', 'kamila', 'angel', 'anyerlis', 'michael', 'edixon', 'carlos', 'keymar', 'joel'].each do |name|
User.create(
email: "#{name}@gmail.com",
password: '123456')
end
puts 'Users has been created'
owner = User.find_by(email: 'keymar@gmail.com')
info = [
['nis', 'armar planilla de amazon', ['1:1','2:2','3:random']],
['traduccion', 'traducir a aleman las categorias de beauty', ['4:1','5:2','6:random']],
['categorizacion', 'Categorizar los productos del cliente x', ['7:1','8:2','9:random']]
]
info.each do |category, description, participant_set|
Category.create!(
name: category,
description: '--'
)
puts 'Category have been created' if Category.count == 3
participants = participant_set.map do |participant|
id, raw_role = participant.split(':')
role = raw_role == 'random' ? [1,2].sample : raw_role
Participant.new(
user: User.find(id.to_i),
role: role.to_i
)
end
Task.create!(
name: category,
description: description,
category: Category.find_by(name: category),
due_date: Date.today + 2.days,
owner: owner,
participating_users: participants
)
end
puts 'Task have been created' if Task.count == 3
Y mis modelos
class Task
# module for mondoid
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :description, type: String
field :due_date, type: Date
field :code, type: String
belongs_to :category
belongs_to :owner, class_name: 'User'
has_many :participating_users, class_name: 'Participant', dependent: :destroy
# has_many :participants, through: :participating_users, source: :user
has_many :notes, dependent: :destroy
validates :name, :description, presence: true
validates :name, uniqueness: {case_sensitive: false}
validate :due_date_validity
before_create :create_code
after_create :send_email
accepts_nested_attributes_for :participating_users, allow_destroy: true
def participants
participating_users.includes(:user).map(&:user)
end
def due_date_validity
return if due_date.blank?
return if due_date > Date.today
errors.add :due_date, 'La fecha no puede estar en el pasado'
end
def create_code
self.code = "#{owner_id}#{Time.now.to_i.to_s(36)}#{SecureRandom.hex(8)}"
end
def send_email
return
return unless Rails.env.development?
(participants + [owner]).each do |user|
ParticipantMailer.with(user: user, task: self).new_task_mailer.deliver!
end
end
end


