Hello, If I have the following model: class MyModel < ActiveRecord::Base def self.some_calculations(id) ..... end end Is it possible to use MyModel.find() and order the results by my defined method self.some_calculations(id)? -- Thanks in advance, Justas -- Posted via ruby-forum.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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 11, 2007, at 11:52 AM, Justas wrote:> If I have the following model: > > class MyModel < ActiveRecord::Base > def self.some_calculations(id) > ..... > end > end > > Is it possible to use MyModel.find() and order the results by my > defined > method self.some_calculations(id)?To accomplish that you need to fetch all the involved rows, and order the objects in Ruby land: @models = MyModel.find_all_by_foo(foo).sort {|a, b| ... } Of course that may be too expensive, depending on the amount of data. If that was the case, you could consider maintaining those calculations via AR callbacks and cache them in some column. That way, you''d delegate ordering to the database. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yes, why not? class MyModel < ActiveRecord::Base def self.some_calculations(id) find(:all, :conditions => [''id != ?'', id], :order => ''rand()'') end end>> MyModel.some_calculations(66)OR drop the self and have an instance method.... class MyModel < ActiveRecord::Base def some_calculations find(:all, :conditions => [''id != ?'', id], :order => ''rand()'') end end>> MyModel.find(66).some_calculationsOn Mar 11, 6:52 pm, Justas <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > If I have the following model: > > class MyModel < ActiveRecord::Base > def self.some_calculations(id) > ..... > end > end > > Is it possible to use MyModel.find() and order the results by my defined > method self.some_calculations(id)? > > -- > Thanks in advance, > Justas > > -- > Posted viahttp://www.ruby-forum.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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 11, 2007, at 3:10 PM, eden li wrote:> class MyModel < ActiveRecord::Base > def self.some_calculations(id) > find(:all, :conditions => [''id != ?'', id], :order => ''rand()'') > end > endJustas wants to order the rows according to the result of applying some_calculations() to them. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---