Rails Default Scope Ordering

1k Views Asked by At

I'm trying to add a default scope to a model that I have where I need it to first order asc by one attribute and then order ascending by a delegated attribute on another model.

I have this so far:

  delegate :name, :location_1, :location_2, :location_3, :location_4,
    to: :park,
    allow_nil: true,
    prefix: true

  default_scope order('coaster_sort ASC').order('park_name ASC')

I keep getting an error stating that park_name does not exist, (which is true as it's not on that model) but it is delegated.

Any ideas on how to fix this?

1

There are 1 best solutions below

4
On

You would need to do joins on the associated table in order to order by it. I haven't tested this, but something like:

default_scope joins(:other_model).order('coaster_sort ASC').order('<other_model>.park_name ASC')

I haven't tested the above code, but I've done this before... The order symbols may need to be adjusted for your code.