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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Zack Chandler
2007-Feb-06 19:29 UTC
Re: How to filter ActiveRecords.find using a facade column?
On 2/6/07, Mike <mguzman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Zack, I supposed that, it''s what I''m going to do. Thank you On Feb 6, 1:29 pm, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2/6/07, Mike <mguzm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > 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 > > 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 Chandlerhttp://depixelate.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---