Forum: Ruby on Rails Custom sort scope for ActiveRecord search

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8849b485516ef2e739a1e8394acb2edd?d=identicon&s=25 eugene miretsky (Guest)
on 2012-05-25 10:11
(Received via mailing list)
Hi,

Here is my problem. I store some of my data in a MYSQL DB and some of it
in
Redis. In my search I need to be able to combine results from both.
Right now my code looks like this:
scope = User.scoped({})
cope = scope.where("users.gender = ?", gender)
cope = scope.where("users.age = ?", age)
etc..
I want to take the result and either sort it using the data that's in
Redis. It is possible do create a scope for that?
The best I could come up with was users = User.search().sort_by {|user|
...} . The problem with is is that it returns an array instead of an
active
record  associations, so now pagination doesn't work.

Regards,
Eugene
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-05-25 10:14
(Received via mailing list)
On May 25, 8:48am, eugene miretsky <eugene.miret...@gmail.com> wrote:
> Redis. It is possible do create a scope for that?
> The best I could come up with was users = User.search().sort_by {|user|
> ...} . The problem with is is that it returns an array instead of an active
> record associations, so now pagination doesn't work.
>

This kind of stuff never works very nicely in my past experience - you
end up having to fetch "too many" records from one of your datasources
because you don't know what ordering the other one is going to impose.
If you're using will paginate you should be able to paginate arrays if
you do require 'will_paginate/array'

Fred
This topic is locked and can not be replied to.