1

I've been doing some edits to an output when working with some records and i've ran into a problem where the sorting option no longer is working.

Initially with how things were created when I would do a @record.class the output was Record::ActiveRecord_Relation < ActiveRecord::Relation

However now after making my changes the class has changed. So now when I run a @record.class the output is Array < Object.

I think this is the root reason why i've ran into this problem.

My code is

@q = @records.search(params[:q])
@records = @q.result(distinct: true).select { |k,_v| k[:disabled] }
@records = @records.order("records.updated_at desc").page(params[:page] || 1).per(params[:per] || 30)

The error I am getting is NoMethodError: undefined methodorder' for #`

Now i've tried to workout around this by

@records = @records.sort_by("records.updated_at desc")
@records = @records.paginate.page(params[:page] || 1).per(params[:per] || 30)

However the error with the first line of my solution is @records = @records.sort_by("records.updated_at desc") I'm not able to get past that line in case there is an error with my paginate solution.

If anyone could take a look, I would really appreciate it!

2
  • It would be useful if you shared where the search method is coming from (and if you wrote it, the code). Commented Mar 21, 2017 at 16:41
  • What is the reason that you use select to select certain records (which loads all record from the database) instead of using where and make only load matching records from the database? That would be much faster. Commented Mar 21, 2017 at 16:41

3 Answers 3

1

your @records is a Ruby array, not an ActiveRecord "array"
You can try @records.sort_by(&:updated_at) to sort your array

Sign up to request clarification or add additional context in comments.

Comments

1

The issue is that you are calling result, which is causing the SQL query to be executed and data returned from the database. You don't want to do that if you want to continue forming your query using the Arel AST that ActiveRecord uses. You want something closer to this (you'll likely need to adapt):

r = records.where(some_param: some_value, disabled: true) \
           .order(updated_at: :desc) \
           .page(params[:page] || 1).per(params[:per] || 30)

Comments

1

The signature you the methods you use look like you are using ransack.

Instead of loading all records into memory and selecting matching records in Ruby it might be much faster to only load matching records from database in the first place.

Assuming that disabled is a boolean column in your database, I would advise to change

@q = @records.search(params[:q])
@records = @q.result(distinct: true).select { |k,_v| k[:disabled] }
@records = @records.order("records.updated_at desc").page(params[:page] || 1).per(params[:per] || 30)

to

@q = @records.where(disabled: true).search(params[:q])
@records = @q.result(distinct: true)
@records = @records.order("records.updated_at desc").page(params[:page] || 1).per(params[:per] || 30)

2 Comments

I really love your concerns with this one, as i have around 9,000 records to work with all together, and it does take a little time to load sometimes disabled is a boolean, however when I looked into your approach, I wind up with undefined method 'where' for #<Ransack::Search:0x007ff9e671fd58> Is there a way around this?
I updated my answer. There where needs to be before all ransack methods in the chain.

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.