<Child's Parent> can't be blank while using accepts_nested_attributes_for (Rails 4)

I found another solution. You can use the inverse_of option within has_many and belongs_to:

class Contact < ActiveRecord::Base
  has_many :goals, inverse_of: :contact
end

class Goal < ActiveRecord::Base
  belongs_to :contact, inverse_of: :goals
  validates_presence_of :title, :due_date, :contact
end

I don't know exactly why it works, but I believe that is because it allow that contact_id is being correctly set, before Goal validation.

I found the solution reading this blog post: http://homeonrails.com/2012/10/validating-nested-associations-in-rails/


The clue is in the error:

Goals contact can't be blank

In your Goal model, you have the following validation:

validates_presence_of :title, :due_date, :contact_id

Try removing contact_id to test if it will accept or not. If it does accept, it means you're not passing the contact_id through your params to the model

We use inherited resources, which appends the parent_id automatically (if you use their belongs_to method). If you're passing the params through accepts_nested_attributes_for, I'd recommend just removing the contact_id validation, as I think it will be added as part of the process