Consider this code:
class Sheet
has_many :songs
end
class Song
belongs_to :sheet
end
sheet = Sheet.find(1)
sheet.songs << Song.find(5)
sheet.songs << Song.find(10)
sheet.songs << Song.find(8)
Now the problem here is that when I run the code below, it returns me a list of songs for that sheet BUT they are not in the same order I added them in (5,10,8). It actually gives me the songs ordered by name.
songs = Sheet.find(1).songs
How can I keep the initial order of the songs?
In order to have a default order, you can do a few things.
Option a)
Options b)
I would not suggest doing default scope as it tends to cause issues with more associations.
Sorry, I think I read that wrong. If you ment keep the order to which it was added to the Sheet model, you would need a pivot table of some nature — creating a has_many :through
It would turn into a: