0

I have an app that manages images associated with buses. My problem is that I can't figure out how to sort an each block by a particular BusImage attribute, :thumb_order instead of the BusImage id.

A Bus has many BusImages.

The instance variables are defined in the Bus controller under Show as...

@bus = Bus.find(params[:id]
@bus_images = BusImage.find(:all, :order => 'thumb_order asc')

And I'm calling these instance variables in the Bus Show page like this:

@bus.bus_images.each do |bus_image|
  bus_image.thumb_order
  (other attributes, etc)
end 

But no matter how I change the :order method on the @bus_images it doesn't affect the view at all. When I tried using the method on @bus, because Buses don't have thumb_order column, just stares at me blankly.

Alaric
  • 229
  • 1
  • 12

2 Answers2

0

I see no link between @bus_images and your next bloc @bus.bus_images. These are 2 completely different variables.

If you want to sort your bus images, try this :

@bus.bus_images.sort_by(&:thumb_order').each do |bus_image|

Or set a default order on your BusImage model, as Default sort order for a rails model?

Community
  • 1
  • 1
pierallard
  • 3,326
  • 3
  • 21
  • 48
0

I find this syntax a little easier to remember, but take your pick:

@bus.bus_images.sort_by{|bi| bi.thumb_order}
Erica Tripp
  • 316
  • 2
  • 8