0

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:

  1. I need a reference to my ChallengeList id (which is not insurmountable, but not pretty either)
  2. I can only reference existing Challenge ids

So what can I do?

Maarten
  • 6,894
  • 7
  • 55
  • 90
  • Are you trying to change the Challenge and the ChallengeList in the same form? Yea, that's going to get a bit ugly. Maybe you show allow reordering on the show page using a jQuery plugin, and no reordering on the edit page. – Farley Knight Nov 27 '13 at 16:42
  • That's what I'm after! I found [this question](http://stackoverflow.com/questions/2182428/rails-nested-form-with-has-many-through-how-to-edit-attributes-of-join-model?rq=1), which looks to be about the same thing I'm trying to achieve. So I'mma try that out, and report my findings. – Maarten Nov 28 '13 at 01:15

1 Answers1

0

Like in this question:

Set up model dependencies like so:

class ChallengeList < ActiveRecord::Base
  has_many :challenge_lists_challenges, :dependent => :destroy
  has_many :challenges, :through => :challenge_lists_challenges
  accepts_nested_attributes_for :challenge_lists
end

class ChallengeListsChallenge < ActiveRecord::Base
  default_scope :order => 'position'
  belongs_to :challenge
  belongs_to :challenge_list
  acts_as_list :scope => :challenge_list
  accepts_nested_attributes_for :challenges
end

And a doubly nested form like so:

= form_for @challenge_list do |f|
  = f.fields_for :challenge_lists_challenges do |links_form|
    .field
      = links_form.number_field :position
      %br/
    = links_form.fields_for :challenge do |challenge_form|
      .field
        = challenge_form.text_field :description
    = challenge_form.text_field :id

And it should work.

Community
  • 1
  • 1
Maarten
  • 6,894
  • 7
  • 55
  • 90