Using act_as_list with has_many: through the rails

I have a rails application that I am trying to configure using sorted lists using the act_as_list plugin. The position field in db is updated, but when the page is displayed, the order is not taken into account. I think I'm looking for some help.

Here are my models ...

class QuestionMembership < ActiveRecord::Base
  belongs_to :form
  belongs_to :question
  acts_as_list
end

class Form < ActiveRecord::Base
  has_many :question_memberships
  has_many :questions, :through => :question_memberships
end

class Question < ActiveRecord::Base
  has_many :question_memberships
  has_many :forms, :through => :question_memberships
  acts_as_list
end

And a sloppy browsing code that gives me a list ...

<% @form.question_memberships.each do |qm| %>
  <% q_id = "question_#{qm.id}" %>
  <li class="question" id=<%= q_id %> >
    <div style="color: #999; font-size: 8pt">
      <%=h qm.question.content %>
    </div>
  </li>
  <%= draggable_element(q_id, :revert=>true) %>
<% end %>

Drag and drop works to reorder. The position value is updated in the database for QuestionMembership objects, and the page actually shows the correct reordering. The problem is that when you reload the page, it defaults to whatever order it looks like. I think it by default sets the question id for the order instead of the question_membership position, but I'm not sure.

, QuestionMembership?

+5
4

:

class QuestionMembership < ActiveRecord::Base
  belongs_to :form
  belongs_to :question
  acts_as_list :scope => :form
end

class Form < ActiveRecord::Base
  has_many :question_memberships, :order => "position"
  has_many :questions, :through => :question_memberships
end

class Question < ActiveRecord::Base
  has_many :question_memberships, :order => "position"
  has_many :forms, :through => :question_memberships
  acts_as_list :scope => :form
end
+8

, . : order : scope .

+3

. , , act_as_list "". , Rails , .

, , , , "position".

, , , , , : scope! "" "", . act_as_list "" , "QuestionMembership" ( ).

, , , , , - .

+2

Rails 2.3 , :

acts_as_list :scope => <scope clause>
default_scope :order => :position

Note that this use of the area is different from what is used for acts_as_list, which decides which area of โ€‹โ€‹the list to.

+1
source

All Articles