GithubHelp home page GithubHelp logo

Comments (4)

moiristo avatar moiristo commented on September 3, 2024

I guess it might have to do with the way simple_form accesses the associations. Have you verified that the objects are in fact in the cloned object? How are you rendering the :pockets and items fields anyway? Can you show me the backpack form?

from deep_cloneable.

aldrinmartoq avatar aldrinmartoq commented on September 3, 2024

I have a very similar problem, I need to clone a Form record without saving it, it has a many-to-many relationship to Field records, and the join table has attributes like required and position of the field in the form.

class Form
  # attributes :name
  has_many :form_fields
  has_many :fields, through: :form_fields

class Field
  # attributes :name
  has_many :form_fields
  has_many :forms, through: :form_fields

class FormField
  # attributes :position, :required
  belongs_to :field
  belongs_to :form
# Original form
2.3.3 :035 > form = Form.find 84
  Form Load (0.5ms)  SELECT  "forms".* FROM "forms" WHERE "forms"."id" = $1 LIMIT 1  [["id", 84]]
+----+------+---------------------------+---------------------------+
| id | name | created_at                | updated_at                |
+----+------+---------------------------+---------------------------+
| 84 | OK   | 2017-04-08 13:12:35 -0300 | 2017-04-08 13:12:35 -0300 |
+----+------+---------------------------+---------------------------+
1 row in set
2.3.3 :036 > form.form_fields
  FormField Load (0.6ms)  SELECT "form_fields".* FROM "form_fields" WHERE "form_fields"."form_id" = $1  [["form_id", 84]]
+-----+---------+----------+----------+---------------------------+---------------------------+----------+
| id  | form_id | field_id | position | created_at                | updated_at                | required |
+-----+---------+----------+----------+---------------------------+---------------------------+----------+
| 218 | 84      | 45       | 20       | 2017-04-08 13:12:36 -0300 | 2017-04-08 13:12:36 -0300 | false    |
| 219 | 84      | 46       | 30       | 2017-04-08 13:12:36 -0300 | 2017-04-08 13:12:36 -0300 | false    |
| 220 | 84      | 47       | 40       | 2017-04-08 13:12:36 -0300 | 2017-04-08 13:12:36 -0300 | false    |
| 221 | 84      | 57       | 50       | 2017-04-08 13:12:36 -0300 | 2017-04-08 13:12:36 -0300 | false    |
| 222 | 84      | 53       | 10       | 2017-04-08 13:12:36 -0300 | 2017-04-08 13:12:36 -0300 | false    |
| 217 | 84      | 67       | 60       | 2017-04-08 13:12:35 -0300 | 2017-04-08 13:31:01 -0300 | true     |
+-----+---------+----------+----------+---------------------------+---------------------------+----------+
6 rows in set
2.3.3 :037 > form.fields
  Field Load (0.9ms)  SELECT "fields".* FROM "fields" INNER JOIN "form_fields" ON "fields"."id" = "form_fields"."field_id" WHERE "form_fields"."form_id" = $1  [["form_id", 84]]
+----+-----------------------+---------------------------+---------------------------+
| id | name                  | created_at                | updated_at                |
+----+-----------------------+---------------------------+---------------------------+
| 45 | RUT                   | 2017-03-29 16:11:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 46 | Datos de Contacto     | 2017-03-29 16:12:11 -0300 | 2017-03-31 15:56:47 -0300 |
| 47 | Descripción Cliente   | 2017-03-29 16:15:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 57 | Productos y Servicios | 2017-03-30 12:31:22 -0300 | 2017-03-31 12:25:05 -0300 |
| 53 | Nombre Cliente        | 2017-03-30 03:21:21 -0300 | 2017-03-31 12:25:05 -0300 |
| 67 | puntaje               | 2017-04-06 16:57:02 -0300 | 2017-04-06 16:57:02 -0300 |
+----+-----------------------+---------------------------+---------------------------+
6 rows in set

We need to clone the Form record and the associated FormFields, Fields records must not be cloned.

Problems appears because the cloned form can't be saved yet, the user has to change things like form name trough a simple_form (which displays the associated fields and their position/required options).

First try, deep cloning with FormField, it works for the :form_fields association but the :fields association is empty.

# Try 1
2.3.3 :041 > clone1 = form.deep_clone include: :form_fields
+----+------+------------+------------+
| id | name | created_at | updated_at |
+----+------+------------+------------+
|    | OK   |            |            |
+----+------+------------+------------+
1 row in set
2.3.3 :042 > clone1.form_fields
+----+---------+----------+----------+------------+------------+----------+
| id | form_id | field_id | position | created_at | updated_at | required |
+----+---------+----------+----------+------------+------------+----------+
|    |         | 45       | 20       |            |            | false    |
|    |         | 46       | 30       |            |            | false    |
|    |         | 47       | 40       |            |            | false    |
|    |         | 57       | 50       |            |            | false    |
|    |         | 53       | 10       |            |            | false    |
|    |         | 67       | 60       |            |            | true     |
+----+---------+----------+----------+------------+------------+----------+
6 rows in set
2.3.3 :043 > clone1.fields
 => #<ActiveRecord::Associations::CollectionProxy []> 

Trying to deep clone with Field ends with the form_fields association not properly cloned (null position and required attributes)

# Try 2
2.3.3 :045 > clone2 = form.deep_clone include: :fields
+----+------+------------+------------+
| id | name | created_at | updated_at |
+----+------+------------+------------+
|    | OK   |            |            |
+----+------+------------+------------+
1 row in set
2.3.3 :046 > clone2.form_fields
+----+---------+----------+----------+------------+------------+----------+
| id | form_id | field_id | position | created_at | updated_at | required |
+----+---------+----------+----------+------------+------------+----------+
|    |         | 45       |          |            |            | false    |
|    |         | 46       |          |            |            | false    |
|    |         | 47       |          |            |            | false    |
|    |         | 57       |          |            |            | false    |
|    |         | 53       |          |            |            | false    |
|    |         | 67       |          |            |            | false    |
+----+---------+----------+----------+------------+------------+----------+
6 rows in set
2.3.3 :047 > clone2.fields
+----+-----------------------+---------------------------+---------------------------+
| id | name                  | created_at                | updated_at                |
+----+-----------------------+---------------------------+---------------------------+
| 45 | RUT                   | 2017-03-29 16:11:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 46 | Datos de Contacto     | 2017-03-29 16:12:11 -0300 | 2017-03-31 15:56:47 -0300 |
| 47 | Descripción Cliente   | 2017-03-29 16:15:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 57 | Productos y Servicios | 2017-03-30 12:31:22 -0300 | 2017-03-31 12:25:05 -0300 |
| 53 | Nombre Cliente        | 2017-03-30 03:21:21 -0300 | 2017-03-31 12:25:05 -0300 |
| 67 | puntaje               | 2017-04-06 16:57:02 -0300 | 2017-04-06 16:57:02 -0300 |
+----+-----------------------+---------------------------+---------------------------+
6 rows in set

I tryed adding both of them in the include, but they end with duplicated entries in form_fields.

# Try 3
2.3.3 :048 > clone3 = form.deep_clone include: [:form_fields, :fields]
+----+------+------------+------------+
| id | name | created_at | updated_at |
+----+------+------------+------------+
|    | OK   |            |            |
+----+------+------------+------------+
1 row in set
2.3.3 :049 > clone3.form_fields
+----+---------+----------+----------+------------+------------+----------+
| id | form_id | field_id | position | created_at | updated_at | required |
+----+---------+----------+----------+------------+------------+----------+
|    |         | 45       | 20       |            |            | false    |
|    |         | 46       | 30       |            |            | false    |
|    |         | 47       | 40       |            |            | false    |
|    |         | 57       | 50       |            |            | false    |
|    |         | 53       | 10       |            |            | false    |
|    |         | 67       | 60       |            |            | true     |
|    |         | 45       |          |            |            | false    |
|    |         | 46       |          |            |            | false    |
|    |         | 47       |          |            |            | false    |
|    |         | 57       |          |            |            | false    |
|    |         | 53       |          |            |            | false    |
|    |         | 67       |          |            |            | false    |
+----+---------+----------+----------+------------+------------+----------+
12 rows in set
2.3.3 :050 > clone3.fields
+----+-----------------------+---------------------------+---------------------------+
| id | name                  | created_at                | updated_at                |
+----+-----------------------+---------------------------+---------------------------+
| 45 | RUT                   | 2017-03-29 16:11:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 46 | Datos de Contacto     | 2017-03-29 16:12:11 -0300 | 2017-03-31 15:56:47 -0300 |
| 47 | Descripción Cliente   | 2017-03-29 16:15:18 -0300 | 2017-03-30 12:31:22 -0300 |
| 57 | Productos y Servicios | 2017-03-30 12:31:22 -0300 | 2017-03-31 12:25:05 -0300 |
| 53 | Nombre Cliente        | 2017-03-30 03:21:21 -0300 | 2017-03-31 12:25:05 -0300 |
| 67 | puntaje               | 2017-04-06 16:57:02 -0300 | 2017-04-06 16:57:02 -0300 |
+----+-----------------------+---------------------------+---------------------------+
6 rows in set

Is there any way to achieve this?

Thanks.

from deep_cloneable.

moiristo avatar moiristo commented on September 3, 2024

I'd say it would be best to not use the fields association in this case, as you only want to dup the form_fields. So stick with clone1 = form.deep_clone include: :form_fields and render the fields in your form using clone1.form_fields.map(&:field). Would that work for you?

from deep_cloneable.

aldrinmartoq avatar aldrinmartoq commented on September 3, 2024

@moiristo yes, something similar is what I'm doing but it means the has_many :through relationship is useless… It seems to be something related to rails rather than this gem.

Thank you.

from deep_cloneable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.