Comments (9)
We also have the same problem which make saving nested new forms impossible, it would be super nice if this can be addressed in the near future.
from ancestry.
That can be worked around by making a custom save method that validates all children first and then saves them from the root down. This is pretty simple to do.
A bigger problem is that there is nothing that simulates the ability of accepts_nested_attributes_for - there are neither a children= or children_attributes-method. This end up being pretty icky pretty fast, especially in the case of STI.
from ancestry.
I've migrated to ancestry... to run in this issue :(
Reverted back to acts_as_tree.
from ancestry.
+1
from ancestry.
The children reference the parent id
But it would seem something as common as belongs_to
would implement something similar.
anyone have ideas how acts_as_tree
or other plugins implement this?
from ancestry.
This is still relevant today for nested forms and speeding up the creation of seed files using parent.children.build
.
from ancestry.
As a workaround I use custom validate method to validate children attributes and after_create hook to create children. And by implementing active model serializer I return parent with its children and it works fine. It is not super nice solution, but I think it could work for some use cases.
Example, I have a field and children are options for the field. I use STI FieldComponent as parent which implements has_ancestry and FieldInput and FieldOption as abstract models.
Field Model
attr_accessor :options
validate :options_attributes, if: -> field { field.option_field? && field.options != nil }
after_create :create_options, if: -> field { field.option_field? && field.options != nil }
def create_options
options.each do |option|
FieldOption.create(option.merge({ parent: self }))
end
end
def options_attributes
options.each do |option|
field_option = FieldOption.new(option)
if(!field_option.valid?)
errors.add(:options, field_option.errors )
end
end
end
Field Controller
def create
@field_input = FieldInput.new(field_input_params)
if @field_input.save
render json: @field_input, status: :created, root: 'data'
else
render json: @field_input.errors, status: :unprocessable_entity
end
end
def field_input_params
params.require(:field).permit(:cf_type, :title, :key, :description, :required, :rank, :hidden, :active, options: [ :id, :key, :title, :rank, :active ])
end
Field Serializer
class Api::V1::Admin::FieldInputSerializer < ActiveModel::Serializer
attributes :id, :cf_type, :title, :key, :description, :required, :active, :custom, :hidden, :rank, :fieldable_type, :options
def options
ActiveModelSerializers::SerializableResource.new(object.children, { adapter: :attributes, namespace: Api::V1::Admin }).as_json
end
end
Option Serializer
class Api::V1::Admin::FieldOptionSerializer < ActiveModel::Serializer
attributes :id, :title, :key, :description, :active, :rank, :children
def children
ActiveModelSerializers::SerializableResource.new(object.children, { adapter: :attributes, namespace: Api::V1::Admin }).as_json
end
end
from ancestry.
thank you for sharing. would love it if someone introduced a way to modify the core so this work around is not necessary.
from ancestry.
Any updates to this? I just started using this with Administrate. It works great for exisiting records, but when I tried to create a new one I got this error:
No child ancestry for new record. Save record before performing tree operations.
Any up to date work arounds for this?
from ancestry.
Related Issues (20)
- Multiple root nodes HOT 1
- gui or gem for generic tree editing? HOT 3
- Make it easier to change the serialization format for the ancestry column HOT 11
- Do I still need to write an arrange_as_array routine to get a children inclusive ordered list? HOT 6
- release next version HOT 6
- having trouble displaying tree in select across different models/controllers
- How do i create a nested ancestry drop down collection_select list for rails forms? HOT 1
- [Idea] Remove InstanceMethods mixin HOT 4
- arrange_as_array causing unknown error HOT 7
- no definition for arrange in arrange_as_array HOT 5
- Falseful changelog, incorrect README and broken code in 4.3.0 HOT 23
- ancestor_ids_in_database removed? HOT 2
- Error in MaterializedPath2 descendants SQL? HOT 6
- Configuring Ancestry to use UUID keys HOT 4
- path_ids_in_database removed? HOT 5
- virtual depth_cache HOT 4
- Why is there no leafs method? HOT 1
- STI breaks arrange scoping HOT 3
- add support for virtual parent_id and root_id
- Using a different, non-unique key as the Ancestry ID column HOT 7
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 ancestry.