在导轨通过:使用acts_as_list用的has_many [英] Using acts_as_list with has_many :through in rails

查看:147
本文介绍了在导轨通过:使用acts_as_list用的has_many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,我想设置与使用acts_as_list插件排序列表。在DB的位置字段得到更新,但是当呈现页面时,为了不考虑。我在寻找一些帮助,我猜。

I have a rails app I'm trying to set up with sortable lists using the acts_as_list plugin. The position field in the db is getting updated, but when the page is rendered, the order is not considered. I'm looking for some help, I guess.

下面是我的模型......

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

和马虎的看法code,让我的列表...

And the sloppy view code that gives me the 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 %>

拖放工程的重新排序。在DB的位置值更新为QuestionMembership对象和页面实际显示正确的重新排序。问题是,在重新加载页面,则默认回任何顺序感觉。我觉得它默认的问题ID为顺序,而不是question_membership的位置,但我不知道。

The drag and drop works for the reordering. The position value updates in the DB for the QuestionMembership objects and the page actually shows the reorder correctly. The problem is that on a page reload, it defaults back to whatever order it feels like. I think it defaults to the question id for the order instead of the question_membership position, but I'm not sure.

我怎么可以把它实际上在最初的订单呈现了QuestionMembership的位置领域的任何想法?

Any ideas on how I can make it actually order on the initial render by the position field of the QuestionMembership?

推荐答案

最终版:

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

这篇关于在导轨通过:使用acts_as_list用的has_many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆