ruby on rails - How do I find the .max of an attribute value among a group of different Models? -


Everyone: I'm just open for straight-up refactoring which is getting me to repeat, but

I have a campaign for every contact, in which there are three types of models: Email, Call, and Letter.

When an email (call or letter) is executed for a specific contact, I have a Contact_Email (_or_Call_or_Letter) that corresponds to the contact and model (email_Or_Ol_Operator).

For example, each contact_email coupling has: the date_sent attribute, each Contact_Call and Contact_Letter.

How can I find the latest from all of them << strong>

Here I can find the latest code that I can find Email and my search call and The letter is to rewrite the same code, but then stuck on how to do a .max for all of them:

  def last_email (contact) #get The campaign is related to the contact @ Campaign = search_ampaign_id @last_email = ContactEmail.find (: last, terms = & gt; "contact_id = # {contact.id}" ,: order = & gt; "date_sent DESC") @ Last_call = ContactCall (last :: terms = & gt; "contact_id = # {contact.id}" ,: Order = & gt; "date_sent DESC") @last_letter = ContactLetter.find (: last, terms = & gt; "contact_id = # {contact.id}",: order => "date_sent DESC") # How to get the latest to display all of these? @email_template = Email.find_by_id (@ last_email.email_id) if @ last_email.nil? Return "No email has been sent" Second return @ Last_Embeddatecent.tr_s (long) + link_to ('email sent', @ email_tieplate) and end  

Question 1: With what I have, how can I effectively get the @last_ event, can I find the last email, last call, and the last letter for every contact?

Question 2: How can I remove duplicate code for two models?

Do you have several other associations in the setup < Code> contact referring to other models? Something like this:

  class contacts & lt; ActiveRecord :: Base has has_many: contact_emails has_many: contact_calls has_many: contact_letters end  

If this is the case, you can contact the last_event method at Model:

  Def latest_event [contact_mail, contacts, contacts_lets] .map | Ashok | Assoc.first (: order = & gt; 'date_sent DESC') end.compact.sort_by {| E E.date_sent} .last end  

zero

handling latest_event method Will get zero if there is no associated record. There are some ways in which you can solve it. The first thing to do first is to check for zero with something:

  contact.latest_event & amp; Amp; Contact.latest_event.date_sent  

You can also use it on late versions of Rail / Ruby, which will call the method if it exists:

 < Code> contact.latest_event. Try (: date_sent)  

I do not like to use it because it does not check for zero , but only when the object object is of a method Can answer if you are expecting zero of the object to be zero , but calling a method, which zero answers itself , Then it may cause some interesting errors.

Finally, my favorite method for simple matter is to use gem, which provides object # and . It makes the case safe from top to very small and sometimes saves from calling latest_event :

  contact.latest_event.andand.date_sent  

date_sent , zero and you

To use your example of calling to_s (long) , you can either use & amp; Amp; & Amp; Amp; or and :

  contact.latest_event.andand.date_sent.andand.to_s (: tall)  

Or

  contact.latest_event & amp; Amp; Contact.latest_event.date_sent.to_s (long)  

is the first safe if date_sent can be zero . It can be written without using:

  contact.latest_event & amp; Amp; Contact.latest_event.date_sent & amp; Amp;  

which is rather complicated and cumbersome in my opinion, I would recommend

to contact.latest_event.date_sent.to_s (: tall)

Comments