Forum: Ruby on Rails How to filter ActiveRecords.find using a facade column?

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.
A073d473b34f181a095f7b8f4be931aa?d=identicon&s=25 Mike (Guest)
on 2007-02-06 20:18
(Received via mailing list)
Hello

    I have a ActiveRecord class that has a facade column, this column
calculates some values using several columns of the table, I want to
filter using :conditions at .find, e.g. (Items.Find :all, :conditions
"usage_rate > 5000" (usage_rate is a def in active record class, not a
column in table) obviously at run time it returns an error about SQL
unknown column.  By now I created a column and populate it every time
the query is made, but it is not an optimal solution. So, how can I
filter records found by .find and use this records in paginate or
whatever I need to exposo that data?

Thank you


Miguel Guzman
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-02-06 20:30
(Received via mailing list)
On 2/6/07, Mike <mguzman1@gmail.com> wrote:
> filter records found by .find and use this records in paginate or
> whatever I need to exposo that data?
>
> Thank you
>
>
> Miguel Guzman
>

If you have to paginate off the column then you really should
permanently add the column to the DB (with an index on it).  A simple
migration should take care of this.  Then update this usage column via
a callback on the appropriate AR model or using a DB trigger.

The reason this is the only viable choice is that the only other
option is to find(:all) and then sort in ruby which makes no sense
from a performance perspective.

--
Zack Chandler
http://depixelate.com
A073d473b34f181a095f7b8f4be931aa?d=identicon&s=25 Mike (Guest)
on 2007-02-06 20:33
(Received via mailing list)
Zack,

    I supposed that, it's what I'm going to do.

Thank you
This topic is locked and can not be replied to.