Skip to content

Does cocoon work with self-joins? #634

Description

@ITS-Marcelo

I'm having troubles understanding if cocoon can work with self-joins. I have the following setup:

class Project < ApplicationRecord
  has_many :allowed_items, class_name: "Project", foreign_key: "validator_id", inverse_of: :validator
  belongs_to :validator, class_name: "Project", inverse_of: :allowed_items, optional: true
  accepts_nested_attributes_for :allowed_items, allow_destroy: true

When I try to create a new project and make it have a validator_id of a per-existing project, I get the following error:

SQLite3::ConstraintException: NOT NULL constraint failed: projects.name

This is where I'm getting confused, from looking at the debug logs, it looks like a project is created with the name and target "s" (sorry for my lack of originality in naming). But then a second project is attempted to be created and fails due to not having a name or target specified. The logs specify that a validator_id was given through the form and it value is 131. The second project item that is attempted to be created has also has a validator_id assigned of value 217 (which I am assuming it is the recently created project). However, I don't want to create two projects. I want to create a project and associated to an existing project. I'll include the log message below:

Started POST "/projects" for 127.0.0.1 at 2025-01-21 10:29:39 -0800
Processing by ProjectsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "project"=>{"name"=>"s", "target"=>"s", "allowed_items_attributes"=>{"1737484178511"=>{"validator_id"=>"131", "_destroy"=>"false"}}}, "commit"=>"Create Project"}
  TRANSACTION (0.0ms)  begin transaction
  ↳ app/controllers/projects_controller.rb:44:in `block in create'
  Project Create (0.5ms)  INSERT INTO "projects" ("name", "target", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "s"], ["target", "s"], ["created_at", "2025-01-21 18:29:39.164287"], ["updated_at", "2025-01-21 18:29:39.164287"]]
  ↳ app/controllers/projects_controller.rb:44:in `block in create'
  Project Create (0.3ms)  INSERT INTO "projects" ("created_at", "updated_at", "validator_id") VALUES (?, ?, ?)  [["created_at", "2025-01-21 18:29:39.166303"], ["updated_at", "2025-01-21 18:29:39.166303"], ["validator_id", 217]]
  ↳ app/controllers/projects_controller.rb:44:in `block in create'

Any feedback would appreciated. Cheers.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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