Search code examples
ruby-on-rails-2

Use :order with find_by_id


In my Rails application I have a Device and a DevicePort class with a one to many relationship between them.

In my device controller I get a list of all the ports belonging to that device but I can't seem to order them:

@device_ports = Device.find_by_id(params[:id], :order => 'id ASC').device_ports

This doesn't seem to work and doesn't give any syntax errors either.

In the view I am simply doing:

<% @device_ports.each do |device_port| %>
    ...
<% end %>

Is anyone able to explain why the ports aren't being sorted by id?


Solution

  • If you want to order device_ports, I'd do something like this:

    @device_ports = Device.find(params[:id]).device_ports.all(:order => 'id ASC')