Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:19:31+00:00 2026-06-17T22:19:31+00:00

I’ve got a rather simple setup here: A Doc model, a Publication model, and

  • 0

I’ve got a rather simple setup here: A Doc model, a Publication model, and an Article model.

doc.rb

class Doc < ActiveRecord::Base
  attr_accessible :article_ids,:created_at, :updated_at, :title
  has_many :publications, dependent: :destroy
  has_many :articles, :through => :publications, :order => 'publications.position'
  accepts_nested_attributes_for :articles, allow_destroy: false
  accepts_nested_attributes_for :publications, allow_destroy: true
end

publication.rb

class Publication < ActiveRecord::Base
  attr_accessible :doc_id, :article_id, :position
  belongs_to :doc
  belongs_to :article
  acts_as_list
end

article.rb

class Article < ActiveRecord::Base
  attr_accessible :body, :issue, :name, :page, :image, :article_print, :video, :id
  has_many :publications
  has_many :docs, :through => :publications
end

The Doc form lets users select and order a number of articles:

...
<% @articles.each do |article| %>
    <span class="handle">[drag]</span> 
    <%= check_box_tag("doc[article_ids][]", article.id, @doc.articles.include?(article), :class => "article_chooser" ) %> 
    <a id="<%= article.id %>" class="name"><%= article.name %></a>
<% end %>
...

This Coffeescript saves the order on drag:

jQuery ->
  $('#sort_articles').sortable(
    axis: 'y'
    handle: '.handle'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
  );

And this is the sort method in docs_controller.rb:

def sort
  Article.all.each_with_index do |id, index|
    Publication.update_all({position: index + 1}, {id: id})
  end
render nothing: true
end

I’ve followed this sortable lists Rails cast and it all works well until I update a Doc record because re-ordering isn’t saved on update. I’ve come to the conclusion that it’s because my sortable field is on the association table (i.e., publications), but because of the way the app works it has to be.

I’ve been doing some research here and found this question whose answer comes close, but because I’ve got a Coffeescript action saving the record in the first place it doesn’t work.

Any help would be excellent, I’m really stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:19:33+00:00Added an answer on June 17, 2026 at 10:19 pm

    Because I struggled for a long time with this I’m putting my stupidly simple solution here. Hopefully it helps someone else.

    It’s as simple as deleting the old join table records on update before the changes are saved, i.e.:

    docs_controller.rb

    def update
      @doc = Doc.find(params[:id])
      @doc.publications.destroy_all
      respond_to do |format|
        if @doc.update_attributes(params[:doc])
          format.html { redirect_to share_url(@doc) }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @doc.errors, status: :unprocessable_entity }
        end
      end
    end
    

    Boom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.