0

Taking the example of spies and missions with a many to many relationship (spies can't be assigned to multiple missions and missions can have 4 or less spies assigned to it), I am able to make a nested form so I can create spies in the same form than the mission. But now, I don't want to create spies in the mission's form but instead, assign already created spies. What would be the best way to do this ?

Manspider
  • 353
  • 3
  • 17

1 Answers1

0

Replace the part of the form that creates spies with one that fetches them by quoting a unique identifier, such as their name (or however they are known.)

You can create the array to search through in the controller.

@spies = Spy.all

If there are only a few of them, I'd use a select tag. If there are too many for that, let the user type it in and use ajax to confirm and autocomplete it.

<%= select(:spy, :spy_id, @spies) %>
Sekalf Nroc
  • 457
  • 3
  • 7
  • Very helpfull thanks, but my spies and mission models won't update anymore. Mission.spies is empty i don't know why – Manspider Feb 24 '17 at 11:50
  • You are welcome. It is very unlikely that this new issue is related to this my answer or, in fact, this question. If you cannot solve it, I advise you to create a new question. Have you run any `rake/rails db:` commands recently? That may have caused it. – Sekalf Nroc Feb 24 '17 at 17:07
  • I did run rake commands. I pretty much solved my problem following this thread http://stackoverflow.com/questions/24642005/rails-association-with-multiple-foreign-keys – Manspider Feb 24 '17 at 17:29