I have a Rails 4 app, with models Challenge
and ChallengeList
. It's a many-to-many relationship, so I also have a join table with model ChallengeListsChallenge
. I defined this last model because I want my ChallengeLists
to be ordered lists, and so used it to exploit acts_as_list
:
class ChallengeList < ActiveRecord::Base
has_many :challenge_lists_challenges, :dependent => :destroy
has_many :challenges, :through => :challenge_lists_challenges
accepts_nested_attributes_for :challenges
end
class ChallengeListsChallenge < ActiveRecord::Base
default_scope :order => 'position'
belongs_to :challenge
belongs_to :challenge_list
acts_as_list :scope => :challenge_list
end
This works fine.
In my HTML, I have a form that allows the user to define a new ChallengeList
. It has a nested form for Challenges
:
= f.fields_for :challenges do |challenge_builder|
.field
= challenge_builder.text_field :description
But I would also like the user to be able to change the position. So I thought I'd be smart, add a field for position:
= challenge_builder.text_field :position
Of course, this doesn't work, because 'position' is set on join items, not Challenge
items.
Having a nested form for ChallengeListsChallenges
would give me access to the position, but is not cool because:
- I need a reference to my
ChallengeList
id (which is not insurmountable, but not pretty either) - I can only reference existing
Challenge
ids
So what can I do?