views:

16

answers:

2

I have a model called Contact.

I have added the following method:

 def all_completed_events
   # for a given Contact, return all contact_events records that exist and where sugarcrm = false
   return (self.contact_letters + self.contact_postalcards + self.contact_emails + self.contact_voicemails + self.contact_calls)
 end

What is missing from these is that I only want self.contact_letters.find(:conditions => "sugarcrm = false") to be the ones selected (sugarcrm is boolean). (obviously this is applied across the other models, contact_letters, contact_emails, etcetera)

How do I do that?

A: 

Maybe try this:

(self.contact_letters + self.contact_postalcards + self.contact_emails + self.contact_voicemails + self.contact_calls).select {|record| !record.sugarcrm }
jtbandes
A: 

You could also define another named scope and chain it:

named_scope nosugar, :conditions => { :sugarcrm => false }

def all_completed_events
  return self.contact_letters.nosugar + self.contact_postalcards.nosugar + ...
end

Depending on how your ContactEvent model is implemented (is it STI? Are you returning all contact_events or just a specific subset of them?) you could get it down to something more like:

return self.contact_events.nosugar
nfm
ContactEvent isn't a model. I have separate ContactLetter, ContactPostalcard, etcetera. Could I lump everything together and then apply the named scope at the end?
Angela
I think you should be able to leverage off your `all_completed_events` method like this: `self.all_completed_events.nosugar`. Not 100% sure though - need a rails console to test it!
nfm