Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Reording ActiveRecord Objects

Hey everyone! I have a list of ActiveRecord Objects I am trying to reorder by the lowest BPM to the highest. The last 5 objects I want to be in reverse from largest to the smallest. I've tried a few things but the issue is once I get what I want I can know longer access the attributes because it is now an Array. I got something close...I think... this is what I have...

@playlist = Playlist.find(params[:id])
     sorted_songs = @playlist.songs.order(tempo: :asc)
     warm_up = sorted_songs
     cool_down = sorted_songs.order(tempo: :desc).limit(5) #sorted_songs.reverse  #maybe try 
     @workout_songs = warm_up + cool_down

This is what is returned...<br>

Hey Ya! - Radio Mix / Club Mix - OutKast | TEMPO: 79<br>

Fuego - Phish | TEMPO: 83<br>

Started From The Bottom - Drake | TEMPO: 86<br>

Free - Phish | TEMPO: 86<br>

Drop It Like It's Hot - Snoop Dogg | TEMPO: 91<br>

This Is How We Do It - Montell Jordan | TEMPO: 103<br>

Wombat - Phish | TEMPO: 104<br>

Hot In Herre - Nelly | TEMPO: 107<br>

SexyBack - Justin Timberlake | TEMPO: 116<br>

Wrecking Ball - Miley Cyrus | TEMPO: 119<br>

Blurred Lines - Robin Thicke | TEMPO: 120<br>

Up N' Down - Britney Spears | TEMPO: 120<br>

Born This Way - Lady Gaga | TEMPO: 123<br>

California Gurls - feat. Snoop Dogg - Katy Perry | TEMPO: 125<br>

We Found Love - Rihanna | TEMPO: 127<br>

I Wanna Go - Britney Spears | TEMPO: 129<br>

Dark Horse - Katy Perry | TEMPO: 131<br>

Big Pimpin' - JAY Z | TEMPO: 138<br>

Partition - BeyoncΓ© | TEMPO: 175<br>

Gooey - Glass Animals | TEMPO: 183<br>

Hey Baby - No Doubt | TEMPO: 187<br>

Hey Ya! - Radio Mix / Club Mix - OutKast | TEMPO: 79<br>

Fuego - Phish | TEMPO: 83<br>

Started From The Bottom - Drake | TEMPO: 86<br>

Free - Phish | TEMPO: 86<br>

Drop It Like It's Hot - Snoop Dogg | TEMPO: 91<br>

As you can see it repeated my first songs instead of giving me the last 5 songs (Dark Horse - Hey Baby).

Can anyone help me??? This is a toughy!

Thanks!

1 Answer

Alan Johnson
Alan Johnson
7,625 Points

How you're doing this is probably how I would. Unfortunately there's not a great way to combine the results of two different ActiveRecord results. Pulling them into arrays is a pretty normal way to handle things, though. Are you running into any specific issues with this technique?