0

System

[2017-11-13 18:34:46] INFO  WEBrick 1.3.1
[2017-11-13 18:34:46] INFO  ruby 1.9.3 (2012-02-16) [x86_64-linux]

Model in Ror, that working (currenct version)

class Expenditure < ActiveRecord::Base
...
  class << self
    def arrange
      super(:order => :name)
    end
...

I tried to add one more field for change order way. "sort_order". I cannot find method arrange in ActiveRecord, which was redifyned I have tried unsuccesfully

super(:order => :sort_order, :name)

...

super.order(:sort_order, :name)
3
  • which attributes do you want to use for ordering? Commented Nov 13, 2017 at 15:54
  • I whant add "sort_order" before "name" ordering, now I have end of query like this: ... ORDER BY (case when expenditures.ancestry is null then 0 else 1 end), expenditures.ancestry, name, that is why I have to stay use super Commented Nov 13, 2017 at 16:05
  • I have no Idia, where is arrange method from Commented Nov 13, 2017 at 16:10

2 Answers 2

0

arrange method from ancestry gem. It's https://github.com/stefankroes/ancestry/blob/8482196e32ac46a9d89c5e3b2d7080e4b830b1d0/lib/ancestry/class_methods.rb#L31

    # Arrangement
    def arrange options = {}
      # Get all nodes ordered by ancestry and start sorting them into an empty hash
      arrange_nodes self.ancestry_base_class.reorder(options.delete(:order)).where(options)
    end
Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand, how can I send two parameters for sorting. Parameters seems like Hahs, But didn't work like (:order => :sort_order, :order => :name) or `({:order=>:sort_order, :order=>:name})'
you can do this with :order => "sort_order ASC, name ASC" .More examples you can find here stackoverflow.com/questions/3587776/…
Yes, That is it. Thank you very match. super(:order => "sort_order, name"). Your link was very usefull
0

Not sure if I understood your question properly, but I think you wanted to use self (not super 'cause there is no arrange method defined in AR) with order method. If you want to order it by sort_order and name in ascending order your method should look like this:

def arrange
  self.order(:sort_order => :asc, :name => :asc)
end

you can even omit self and just use self.order(:sort_order => :asc, :name => :asc)

1 Comment

No, I need super

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.