Rails:可排序的has_and_belongs_to_many关系

时间:2013-01-08 00:18:02

标签: jquery ruby-on-rails-3 jquery-ui sorting has-and-belongs-to-many

鉴于这种关系:

class Doc < ActiveRecord::Base
  has_and_belongs_to_many :articles
end

class Article < ActiveRecord::Base
  has_and_belongs_to_many :docs 
end

您如何使用jQuery UI对Doc中的articles进行排序?我正在关注this教程,在完成此迁移之前,这一切都很顺利:

rails g migration add_position_to_faqs position:integer

因为当我已经有了一个关系表时,它看起来像是双倍的,因此:

create_table "articles_docs", :id => false, :force => true do |t|
  t.integer "article_id"
  t.integer "doc_id"
end

add_index "articles_docs", ["article_id", "doc_id"], :name => "index_articles_docs_on_article_id_and_doc_id", :unique => true

有什么想法吗?没有真正理解关系表并没有帮助我解决这个问题。

1 个答案:

答案 0 :(得分:2)

使用has_many :through代替has_and_belongs_to_many。这将为您提供一个中间模型来保存位置列,您将对那些进行排序,而不是对文章进行排序。