1
votes

I have two different models. One called 'Books' and one called 'Author'. In ActiveAdmin, when creating a new 'book' under 'Books', how can I have a dropdown in the form with a list of Authors from the 'Author' model?

I have been searching for a hint all day and haven't managed to come up with anything yet.. Any help would be appreciated!

2

2 Answers

3
votes

alexandrine_greenfelder's answer is a proper solution, but when it comes to bigger amounts of data, this kind of loading entries for selection takes a lot of time.

pluck is much faster in this context:

f.input :author, as: :select, collection: -> { Author.pluck(:full_name, :id) }

Also notу how I use lambda to seed a collection:

-> { Author.pluck(:full_name, :id) }

This way the data will be loaded from database once selection dropdown is clicked, not when the model is initialized (which prevent situations, when new Author instance is added to db, but not present in dropdown yet).

2
votes

Use something like this:

f.input :author, as: :select, collection: Author.all.map { |a| [a.full_name, b.id] }

in form section.