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

  • SEARCH
  • Home
  • 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 6676515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:59:53+00:00 2026-05-26T03:59:53+00:00

I’m working on a Rails plugin that includes a way to modify the order

  • 0

I’m working on a Rails plugin that includes a way to modify the order of associated records in a has_many :through association. Say we have the following models:

class Playlist < ActiveRecord::Base
  has_many :playlists_songs, :dependent => :destroy
  has_many :songs, :through => :playlists_songs
end

class Song < ActiveRecord::Base
  has_many :playlists_songs, :dependent => :destroy
  has_many :playlists, :through => :playlists_songs
end

class PlaylistsSong < ActiveRecord::Base
  belongs_to :playlist
  belongs_to :song
end

If we change the order of a Playlist’s Songs (e.g. @playlist.songs.rotate!), Rails doesn’t touch the records in the playlists_songs table (I’m using Rails 3.1), which makes sense. I’d like to make any call to Playlist’s songs= method save the order of the Songs, though, perhaps by either deleting the relevant existing rows in playlists_songs and creating new ones in the proper order (so that :order => "id" could be used when retrieving them) or by adding a sort:integer column to playlists_songs and updating those values accordingly.

I didn’t see any callbacks (e.g. before_add) that would allow this. In ActiveRecord::Associations::CollectionAssociation, the relevant methods seem to be writer, replace, and replace_records, but I’m lost on what the best next step would be. Is there a way to extend or safely override one of these methods to allow for the functionality I’m seeking (preferably for only specific associations), or is there a different, better approach for this?

  • 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-05-26T03:59:54+00:00Added an answer on May 26, 2026 at 3:59 am

    Have you looked at acts_as_list? It’s one of the most old-school of rails plugins, and is intended to handle this sort of problem.

    Rather than sorting on id, it sorts on a positional column. Then it’s simply a matter of updating the position, rather than the messy business of changing the id or deleting/replacing records.

    In your case, you’d simply add a position integer column to PlayListSong, then:

    class PlayListSong
      acts_as_list :scope => :play_list_id
    end
    

    As you point out in the comments, the methods in acts_as_list work mostly on individual items in the list, and there’s no “reorder” functionality out of the box. I would not recommend tampering with replace_records to do this. It would be cleaner and more explicit to write a method making use of the same position column as the plugin. For example.

    class PlayList
      # It makes sense for these methods to be on the association.  You might make it
      # work for #songs instead (as in your question), but the join table is what's
      # keeping the position.
      has_many :play_list_songs, ... do
    
        # I'm not sure what rotate! should do, so...
    
        # This first method makes use of acts_as_list's functionality
        #
        # This should take the last song and move it to the first, incrementing 
        # the position of all other songs, effectively rotating the list forward 
        # by 1 song.
        def rotate!
          last.move_to_top unless empty?
        end
    
        # this, on the other hand, would reorder given an array of play_list_songs.
        # 
        # Note: this is a rough (untested) idea and could/should be reworked for 
        # efficiency and safety. 
        def reorder!(reordered_songs)
          position = 0
          reordered_songs.each do |song|
            position += 1
    
            # Note: update_column is 3.1+, but I'm assuming you're using it, since
            # that was the source you linked to in your question
            find(song.id).update_column(:position, position)
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.