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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:28:13+00:00 2026-06-15T15:28:13+00:00

I have two models, Song and Show. A Show is an ordered list of

  • 0

I have two models, Song and Show. A Show is an ordered list of Songs, in which the same Song can be listed multiple times.

That is, there should be an ordered array (or hash or anything) somewhere in Show that can contain Song1, Song2, Song1, Song3 and allow re-ordering, inserting, or deleting from that array.

I cannot figure out how to model this with ActiveRecord associations. I’m guessing I need some sort of special join table with a column for the index, but apart from starting to code my SQL directly, is there a way to do this with Rails associations?

Some code as I have it now (but doesn’t work properly):

class Song < ActiveRecord::Base
  attr_accessible :title
  has_and_belongs_to_many :shows
end

class Show < ActiveRecord::Base
  attr_accessible :date
  has_and_belongs_to_many :songs
end

song1 = Song.create(title: 'Foo')
song2 = Song.create(title: 'Bar')
show1 = Show.create(date: 'Tomorrow')

show1.songs << song1 << song2 << song1

puts "show1 size = #{show1.songs.size}" # 3
show1.delete_at(0) # Should delete the first instance of song1, but leave the second instance
puts "show1 size = #{show1.songs.size}" # 2
show1.reload
puts "show1 size = #{show1.songs.size}" # 3 again, annoyingly

Inserting might look like:

show1.songs # Foo, Bar, Foo
song3 = Song.create(title: 'Baz')
show1.insert(1, song3)
show1.songs # Foo, Baz, Bar, Foo

And reordering might (with a little magic) look something like:

show1.songs # Foo, Bar, Foo
show1.move_song_from(0, to: 1)
show1.songs # Bar, Foo, Foo
  • 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-15T15:28:15+00:00Added an answer on June 15, 2026 at 3:28 pm

    My current solution to this is a combination of has_many :through and acts_as_list. It was not the easiest thing to find information on combining the two correctly. One of the hurdles, for example, was that acts_as_list uses an index starting at 1, while the array-like methods created by the ActiveRecord association start at 0.

    Here’s how my code ended up. Note that I had to specify explicit methods to modify the join table (for most of them anyway); I’m not sure if there’s a cleaner way to make those work.

    class Song < ActiveRecord::Base
      attr_accessible :title
      has_many :playlist_items, :order => :position
      has_many :shows, :through => :playlist_items
    end
    
    class PlaylistItem < ActiveRecord::Base
      attr_accessible :position, :show_id, :song_id
      belongs_to :shows 
      belongs_to :songs
      acts_as_list :scope => :show
    end
    
    class Show < ActiveRecord::Base
      attr_accessible :date
      has_many :playlist_items, :order => :position
      has_many :songs, :through => :playlist_items, :order => :position
    
      def song_at(index)
        self.songs.find_by_id(self.playlist_items[index].song_id)
      end
    
      def move_song(index, options={})
        raise "A :to option is required." unless options.has_key? :to
        self.playlist_items[index].insert_at(options[:to] + 1) # Compensate for acts_as_list starting at 1
      end
    
      def add_song(location)
        self.songs << location
      end
    
      def remove_song_at(index)
        self.playlist_items.delete(self.playlist_items[index])
      end
    end
    

    I added a ‘position’ column to my ‘playlist_items’ table, as per the instructions that came with acts_as_list. It’s worth noting that I had to dig into the API for acts_as_list to find the insert_at method.

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

Sidebar

Related Questions

I have two models: Users and Projects. The idea is that Users can follow
I have two models User and Category that have a HABTM association. I would
I have two models and i am trying to show the told credits of
I have two models - category and article. I have pretty much the same
I have two models that has a manytomany relationship with a 'through' table in
I have two models, TreeNode and User. Each user has_one TreeNode, which is the
I have two models in a has_many relationship such that Log has_many Items. Rails
I have two models, Users and Groups. Each group can have many users and
I have two models: Novel has_many :pages Page belongs_to :novel I want to list
I have three models, Poem and Song and User . Users can vote on

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.