We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I was trying to figure out how to use CITIER with accepts_nested_attributes_for such that I could create nested child models dynamically. For example:
accepts_nested_attributes_for
List.new( items_attributes => [ { ItemTypeA => { STUFF }, { ItemTypeB => { STUFF }, ])
I found some information from: http://stackoverflow.com/questions/2553931/can-nested-attributes-be-used-in-combination-with-inheritance (see response 1).
This lead me to a monkey-patch with the following:
class ActiveRecord::Reflection::AssociationReflection < ActiveRecord::Reflection::MacroReflection def build_association(*options, &block) @original_build_association_called = true if options.first.is_a?(Hash) and options.first[:model_type].presence build_klass = options.first[:model_type].to_s.constantize # raises NameError build_klass.new(*options) # raises ActiveRecord::AssociationTypeMismatch else klass.new(*options, &block) end end end
Now I can pass the :model_type param to create specific child instances when creating nested_attributes or from List.build(:model_type => ItemTypeA).
I hope that this helps anyone trying to do the same thing.
The text was updated successfully, but these errors were encountered:
Hey.. looking at this now but just for a nested_attribute on a Root citier model (but adding through sub model form) where do you put this code?
Sorry, something went wrong.
No branches or pull requests
I was trying to figure out how to use CITIER with
accepts_nested_attributes_for
such that I could create nested child models dynamically. For example:I found some information from: http://stackoverflow.com/questions/2553931/can-nested-attributes-be-used-in-combination-with-inheritance (see response 1).
This lead me to a monkey-patch with the following:
Now I can pass the :model_type param to create specific child instances when creating nested_attributes or from List.build(:model_type => ItemTypeA).
I hope that this helps anyone trying to do the same thing.
The text was updated successfully, but these errors were encountered: