0

Using ActiveAdmin 2.9.0, Rails 6 and Postgres 14.6.

I have model Persona that looks like this in the schema:

create_table "personas", force: :cascade do |t|
  t.string "orcid"
  t.string "lastname" 
  t.string "firstname" 
  t.integer "scopi", default: [], null: false, array: true
end

The problem is with array "scopi" that is an array of integers. I can write this attribute fine in the console:

Persona.last.update(scopi: [1,2,3])
Persona.last
=> #<Persona id: 22, creator_id: 14, orcid: "1111-2222-3333-4444", lastname: "tt34", firstname: "aa", created_at: "2023-10-17 23:48:15", updated_at: "2023-10-18 01:29:42", scopi: [1, 2, 3]> 

However in Active Admin, despite using the strong parameters as follows:

permit_params :firstname, :lastname, scopi: [ ]

during PATCH after editing with formtastic in ActiveAdmin resource edit view, I am getting

Unpermitted parameter: :scopi

and the resource's :scopi field is not saved. I believe that this should work because AA's manual states "Any form field that sends multiple values (such as a HABTM association, or an array attribute) needs to pass an empty array to permit_params".

I tried :scopi instead of scopi: [] in the permit_params, but any input entered e.g. 1,2,3 or [1,2,3] gets translated to an empty string and an empty array is saved. Most questions on this topic relate to arrays of nested parameters, but I am working directly with an array column in the model. Other than AA's manual I could not find a working example.

How should I construct the permit_params to make this work? And, I can see ahead that the formtastic input may have problems converting my typed-in input to an array. Should I just use f.input :scopi, as: :string to achieve this? And what format should I use when typing in the array?

3
  • I'm not very familiar with AA but I think its basically just a wrapper around Rails standard strong parameters. Passing a symbol .permit(:foo) will permit any permitted scalar value but that list does not include arrays. .permit(foo: []) will permit an array of scalar values. Commented Oct 18, 2023 at 12:27
  • "Should I just use f.input :scopi, as: :string to achieve this? And what format should I use when typing in the array?". Neither Rails nor AA will automatically deserialize user input in a single formdata key into arrays. If you want to pass arrays as parameters in Rack applications that's done by adding square brackets [] to the name of the inputs. That happens automatically when you use checkboxes or selects. For example Rack::Utils.parse_query("foo[]=1&foo[]=2&foo[]=3"). Commented Oct 18, 2023 at 12:32
  • I think you would most likely get better answers if you described what real world problem this is supposed to solve and how users are actually supposed to interact with it. Array columns are commonly regarded as an anti-pattern as they violate 1NF and there is most likely a better solution if you don't get stuck on the potential XY problem such as creating a separate table and model and using nested attributes. Commented Oct 19, 2023 at 10:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.