Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Embrace duck-typing and make sure that your objects are <strong>polymorphic</strong>. You want your <em>payment</em> items to be <strong>comparable</strong> with <em>maintenance</em> items, in order to sort them.</p> <p>So, suppose you have a <code>Payment</code> and a <code>Maintenance</code> class:</p> <pre><code>module Due include Comparable # Compare this object with another. Used for sorting. def &lt;=&gt;(other) self.due &lt;=&gt; other.due end end class Payment &lt; ActiveRecord::Base include Due alias_method :due, :payment def action "#{name} requires payment" end end class Maintenance &lt; ActiveRecord::Base include Due alias_method :due, :maintenance def action "#{name} requires maintenance" end end </code></pre> <p>See how we create an <code>action</code>, <code>due</code> and <code>&lt;=&gt;</code> method in <strong>both</strong> classes? We also took care to include the Ruby built-in module <code>Comparable</code>. This allows us to do the following:</p> <pre><code># Assuming 'payment' and 'maintenance' are date fields... a = Payment.new :payment =&gt; 3.days.from_now b = Maintenance.new :maintenance =&gt; 2.days.from_now [a, b].sort #=&gt; [b, a] </code></pre> <p>The view then becomes as simple as:</p> <pre><code>&lt;% (@payment_items + @maintenance_items).sort.each do |item| %&gt; &lt;%= item.action %&gt; in &lt;%= distance_of_time_in_words_to_now(item.due) %&gt;&lt;br/&gt; &lt;% end %&gt; </code></pre> <p>I'm sure I haven't got the details of your implementation right, but I hope this gives you an idea of how to approach your problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload