V
V
Vyacheslav Alekseev2015-03-26 06:30:58
ruby
Vyacheslav Alekseev, 2015-03-26 06:30:58

[Ruby,Sinatra,Datamapper] How to select elements by association order by father ID?

Hello!
I have a Mainstack model that has belongs_to :song and a Song model that has n, :mainstacks
I need to select Mainstack.all.songs but sort it by parent Mainstack ID
Mainstack.all(:order => [id] ).songs doesn't work, still sorted by songs.id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Penkovsky, 2015-03-26
@AlexeevVyacheslav

Since you write in ruby, then write correctly
Well, sorting can be done like this

@songs = Song.all(:order => [Song.mainstacks.id.desc])

V
Vyacheslav Alekseev, 2015-03-26
@AlexeevVyacheslav

@Mainstacks = Mainstack.all( :order => [:id.desc])
    @songs = []
    @Mainstacks.each do |mainstack|
      @songs << mainstack.song
    end

I did it, but it seems to me that this is not the best solution, any ideas?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question