Skip to content

RFC - model save hooks and mobility #574

@mrbrdo

Description

@mrbrdo

There seems to be an issue with attr readers in some configuration of model after_create/save hooks. See also original issue mrbrdo/spree_mobility#6

Code triggering the issue (in Spree project if you are familiar):

    after_create :set_root
    after_save :set_root_taxon_name

    private

    def set_root
      self.root ||= Taxon.create!(taxonomy_id: id, name: name)
    end

    def set_root_taxon_name
      return unless saved_change_to_name?
      return if name.to_s == root.name.to_s

      root.update(name: name)
    end

At some point here, Taxon's name (for translation) is set to nil, although a non-nil value was given to base model. It has something to do with the save hooks, it seems. Since I have a validation on the translation model's name presence, the validation fails. Do you have any idea what could be the issue here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions