Skip to content
New issue

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

Handling nested attributes (hash of hashes) #531

Open
PedroHamzehT opened this issue Jun 30, 2022 · 3 comments
Open

Handling nested attributes (hash of hashes) #531

PedroHamzehT opened this issue Jun 30, 2022 · 3 comments
Labels

Comments

@PedroHamzehT
Copy link

Hello! I am wondering how I can set up my interaction to expect has_many nested attributes in the Rails form format.

I have two classes:

class User
  has_many :pets
end

class Pet
  belongs_to :user
end

And when a create a form for the User model and I want to already create their pets, I can just use the fields_for helper to send a pets_attributes hash with others hash (simulating an array).

Example:
The submit will be sent in this format:

"user" => { "pets_attributes"=>{"0"=>{"name"=>"Faisca}, "1" => {"name" => "Cat"}}}

I would like to know how to expect the pets_attributes hash inside of my interaction, knowing that the pets_attributes can have n hashes because it is a has_many association.

@AaronLasseigne
Copy link
Owner

I'm considering solutions for this. See #535.

@r4mbo7
Copy link
Contributor

r4mbo7 commented Jul 25, 2023

A workaround until the gem support this, is to use the option strip: false to the hash (nested attributes wont be validated but it will works):

class CreateUser < ActiveInteraction::Base
  hash :pets_attributes, default: nil, strip: false

@umamaheswari-raj
Copy link

A workaround until the gem support this, is to use the option strip: false to the hash (nested attributes wont be validated but it will works):

class CreateUser < ActiveInteraction::Base
  hash :pets_attributes, default: nil, strip: false

It's works for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants