0

I'm using rails 3.2 and cocoon gem to create new nested-fields, and have this models.

ParteDiario.rb

class ParteDiario < ActiveRecord::Base
  has_many :parte_diario_items, dependent: :destroy
  has_many :task_one, through: :parte_diario_items
  accepts_nested_attributes_for :parte_diario_items, :reject_if => lambda { |a| a[:employee_id].blank? and a[:new_employee].blank? }, :allow_destroy => true
  validates_associated :parte_diario_items
end

ParteDiarioItem.rb

class ParteDiarioItem < ActiveRecord::Base
    belongs_to :employee
    belongs_to :task_one, :class_name => 'ParteDiarioTask', :foreign_key => 'task_one_id'
    validates :employee_id, presence: true, if: "new_employee.nil?"
    validates :new_employee, presence: true, if: "employee_id.nil?"
end

ParteDiarioItem has two columns: employee_id and new_employee (this is a string for inserting a name manually). The idea is that you can create an item using an existing employee, or you can create a new_employee (just writing a name manually). If you create a new employee, empleado_id is nil. That way, in DB i will have every item with an employee_id or with a new_employee_[name]. When one of them is nil, the other shouldn't be.

When submitting the form, only one of those fields is sent (when employee_id input is shown to the user, new_employee input is not). I use cocoon gem to create new nested-fields and everything is working fine (except for validations).

Using this syntax is not working. When submitting a form with an item with 'new_employee input' blank, ParteDiario.valid? is true. And should be false.

Also tried something like this, with same results:

validates :employee_id, presence: true, unless: :new_employee
validates :new_employee, presence: true, unless: :employee_id

Could anyone help me please?

Thanks a lot!

2 Answers 2

3

I've finally found the answer thanks to this question: Rails validations are not being run on nested model

I'm not sure why, but seems that this line was generating the problem

accepts_nested_attributes_for :parte_diario_items, :reject_if => lambda { |a| a[:employee_id].blank? and a[:new_employee].blank? }, :allow_destroy => true

Removing :reject_if... let me validate the model correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

I spent two hours trying to work out why my form wasn't validating and this fixed it. Thanks!
0

The following code may help you:

Validates_presence_of :employee_id, :if => Proc.new{|p| p.new_employee.nil? }
Validates_presence_of :new_employee, :if => Proc.new{ |p| p.employee_id.nil? }

1 Comment

@skunstel Thanks for the answer, but i'm afraid is not working. Sending the form with new_employee input blank is still giving a valid parte_diario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.