Comments (4)
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.
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.
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.
@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)
- rails 4 deep_clone different in development/staging and production HOT 2
- deep_clone not called on has_many_through relation HOT 4
- How to handle :has_rich_text associations in rails 6 HOT 3
- include:do loop also modified the includes HOT 1
- How To Attach Polymorphic Associated Records To A Different Model? HOT 2
- Error: private method `open' called for #<ActiveStorage::Attached::Many HOT 20
- Options argument hash for deep_clone is modified HOT 2
- Question: How to deep clone classes associated to the same class, as in a linked list or tree? HOT 1
- Conditional nested includes HOT 2
- Include all associations without having to specify all of them? HOT 1
- 3.1.0 is not released to rubygems.org? HOT 2
- Using deep clone to extract a subset db from production HOT 2
- Possible unexpected has_and_belongs_to_many behaviour. HOT 2
- Add support for Rails 7
- deep_clone has issues with acts_as_tenant gem how we can make it work ? HOT 1
- Add support for Mongoid HOT 1
- How to handle multiple parents? HOT 1
- Not possible to exclude attribute named 'rank' HOT 2
- Fix documentation for Active Storage usage in Rails 6.1
- problem with counter_cache HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deep_cloneable.