Skip to content
This repository was archived by the owner on Aug 17, 2017. It is now read-only.
This repository was archived by the owner on Aug 17, 2017. It is now read-only.

Set polymorphic association in a manner that Rails picks up on the association add #236

@noahmatisoff

Description

@noahmatisoff

I have a line in one of my models that looks like this:

has_many :notes, as: :notable, dependent: :destroy, after_add: :create_informal_note

I am sending a POST request to my Rails endpoint with JSON like this:

{
  "note": {
    "text": "Test note",
    "notable_id": 1,
    "notable_type": "AnotherModel"
  }
}

Rails & strong_parameters has no problem interpreting this and creating the data. However, due to the way the JSON request body is structured, Rails & strong_parameters simply just set the id and type fields.

This results in Rails not picking up on the added association, causing my after_add association callback not to trigger.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions