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 8179099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:55:39+00:00 2026-06-06T23:55:39+00:00

I’m trying to add acts_as_list to an existing playlists table in my database, which

  • 0

I’m trying to add acts_as_list to an existing playlists table in my database, which stores any playlists that my users might have created. The goal is to allow users to sort their own playlists.

This migration will set a default position for the playlists in the table, but the position will be set in a global context (for example, the position column will be ordered 1-500 for a table with 500 playlists in it):

class AddPositionToPlaylistsTable < ActiveRecord::Migration
  def self.up
    add_column  :playlists, :position, :integer
    Playlist.reset_column_information
    playlist = Playlist.find(:all, :order => "created_at ASC")

    playlist.each_with_index do |p, i|
      p.position = i+1
      p.save
    end

  end

  def self.down
    remove_column :playlists, :position
  end
end

This order would be problematic, because moving an item’s position from 500 to 499 wouldn’t necessarily reflect any change from the user’s perspective (as they might have playlists that have a position of 50, 70, 100, and 500).

Instead, I want to set a position for each playlist in the table in a per-user context (for example, presuming each user has 5 playlists, the position column would have multiple instances of playlists ordered 1-5 for each user).

Is this possible, and if so, how would I modify my migration to accomplish 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-06-06T23:55:40+00:00Added an answer on June 6, 2026 at 11:55 pm

    When updating your playlists position, you only take the user’s playlists into account. So you should do it from the beginning. In your migration’s up-method you could get all playlists per user and then increase the index like you did:

    #....
    User.all.each do |user|
      user_playlists = user.playlists(:order => "created_at ASC")
    
      user_playlists.each_with_index do |p, i|
        p.position = i+1
        p.save
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a view passing on information from a database: def serve_article(request, id): served_article
I have two tables with like below codes: Table: Accounts id | username |

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.