Rails 5.1 Multiple belongs_to associations with optional: true, rollback - 'required association'

1.5k Views Asked by At

I want to use belongs_to parent_model, optional: true assocation for 1 child - and 2 parents. Found this answer: Model belongs_to eiher/or more than one models. Tried that. But with 2 associated parent models one of them causes rollback when saving the child model.

Is there any limitation on belongs_to with multiple parent models and optional: true? Or am I missing some other presence validation? Important to say: I would rather avoid using polymorphic associations in this case.

PrivateAttachment has a file attached using Paperclip. private_attachment.rb

Class PrivateAttachment < ApplicationRecord

  belongs_to :first_class, optional: true
  belongs_to :second_class, optional: true

  has_attached_file :certificate

  validates_attachment_presence :certificate
  validates_attachment_size :certificate, :less_than => 10.megabytes
  validates_attachment_content_type :certificate,
    :content_type => [
      "image/jpg", 
      "image/jpeg", 
      "image/png", 
      "application/pdf",
      "file/txt",
      "text/plain",
      "application/doc",
      "application/msword", 
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "application/vnd.oasis.opendocument.text",
      ]

end

first_class.rb

Class FirstClass < ApplicationRecord

  validates_length_of :message, :maximum => 2000, allow_blank: true

  has_many :private_attachments, dependent: :destroy
  has_many :approvals
  accepts_nested_attributes_for :approvals

end

second_class.rb

Class SecondClass < ApplicationRecord

  has_many :private_attachments, dependent: :destroy

end

When creating PrivateAttachment:

second_class.private_attachments.new(certificate: file)

there's a Rollback on save with a message, despite optional attribute is set to true.

Full error

ActiveModel::Errors:0x00007f8bbe03a380 @base=#PrivateAttachment id: nil, first_class_id: nil, company_id: nil, user_id: nil, doc_type: nil, url: nil, active: nil, blind: nil, permit_period: nil, views: nil, downloads: nil, created_at: nil, updated_at: nil, certificate_file_name: "test.pdf", certificate_content_type: "application/pdf", certificate_file_size: 443632, certificate_updated_at: "2018-07-24 20:18:20", second_class_id: 23>, @messages={:first_class=>["First class required."]}, **@details={:first_class=>[{:error=>:blank}]}>

When creating Attachment with a first_class, everything works just fine, no error.

UPDATE 1---------------------------------------------------------------------

I just realized, that first_class has a child model Approval which has own validation. But at this point I don't understand why should be this deep assocation being taken into consideration when using optional: true ?

approval.rb

class Approval < ApplicationRecord

  belongs_to :job_application

  validates_acceptance_of :operator_approval, :allow_nil => false,

end

Rails 5.1.6. PG DB

0

There are 0 best solutions below