Here''s the situation...
:customer has_many :billing_windows
:billing_window belongs_to :customer
A billing_window can either be open or closed (it is considered open
until its closed_on attribute is set to a datetime). I''m trying to
refactor the class method on Customer that finds all customers with at
least 1 open billing_window. Currently it looks like this...
def self.has_open_billing_window
set = []
Customer.find(:all).each do |customer|
set << customer if customer.billing_windows.find(:all, :conditions
=> [:open => true]).size > 0
end
set
end
For obvious reasons, this is horribly inefficient so I have refactored
that code to:
def self.has_open_billing_window
Customer.find(:all, :joins => :billing_windows, :conditions =>
"billing_windows.closed_on IS NULL")
end
This definitely seems cleaner but I don''t like the fact that the
conditions clause is specific to the database query language. Does
anyone know a better way to accomplish this?
Bob
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Bob wrote: [...]> For obvious reasons, this is horribly inefficient so I have refactored > that code to: > > def self.has_open_billing_window > Customer.find(:all, :joins => :billing_windows, :conditions => > "billing_windows.closed_on IS NULL") > end > > > This definitely seems cleaner but I don''t like the fact that the > conditions clause is specific to the database query language.The clause isn''t specific to the DB query language. It contains only standard SQL, and so should work perfectly in any SQL database. Granted, it won''t necessary work in a non-SQL database, but those are typically structured very differently from SQL databases anyway.> Does > anyone know a better way to accomplish this?You don''t need one.> > BobBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.