0

Given this relationship:

class Doc < ActiveRecord::Base
  has_and_belongs_to_many :articles
end

and

class Article < ActiveRecord::Base
  has_and_belongs_to_many :docs 
end

How would you approach sorting the articles in the Doc with jQuery UI? I was following this tutorial, which was all well and good until it came time to run this migration:

rails g migration add_position_to_faqs position:integer

because it seems like doubling-up when I already have a relationship table, thus:

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

Any thoughts at all? Not really understanding the relationship table isn't helping me figure this out.

t56k
  • 6,769
  • 9
  • 52
  • 115

1 Answers1

2

Use has_many :through instead of has_and_belongs_to_many. That will give you an intermediary model to hold the position column, and you'll sort those rather than sorting the articles.

James Mason
  • 4,246
  • 1
  • 21
  • 26
  • Do you know of any resources or tutorials for a conversion? Everything I can find seems kinda dated and/or unclear. – t56k Jan 08 '13 at 00:47
  • 1
    I don't know of any good tutorials, but it's usually a very simple process. Generate a new model for the join, add the two belongs_to associations to it, change your has_and_belongs_to_manys into has_many :throughs, then fix your migrations (migrate down, delete the habtm migration, then migrate up). – James Mason Jan 08 '13 at 01:03