Skip to main content

accepts_nested_attributes_for and :reject_if

Rails • Asked by Bartosz Michalak

Hello everybody!
I have problem with nested attributes:.

My model:

class MainTransactionElement < ApplicationRecord
    has_one :external_source
    accepts_nested_attributes_for :external_source, :reject_if => :is_external_source_blank?

    def is_external_source_blank?
        is_external_source_checkbox == '0'

During the first load of form, it works. But is one bug:

  1. Checkbox is unchecked and form won't pass the validation and render again.
  2. In next render form doesn't generate nested attributes and I check checkbox.
  3. In next render, nested attributes still doesn't generate, despite the previously marked checkbox.

Any ideas why?

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.