Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay - after struggling on and off with this, I crack it within 10mins of posting to stackoverflow! Typical.</p> <p>To save others from banging their heads against walls, here's what I had wrong:</p> <p>Note should have been:</p> <pre><code>class Note &lt; ActiveRecord::Base has_one :card, :through =&gt; :timeline_item #not timeline_items has_one :timeline_item, :as =&gt; :item end </code></pre> <p>And that was it! I was trying to use the creation methods used in <a href="https://gist.github.com/1242485" rel="nofollow">this article</a>, but actually that's not required.</p> <p>Here's the console output, showing that the sql statements are all using the timeline_items table:</p> <pre><code>1.9.2-p290 :009 &gt; c = Card.find(547) Card Load (0.3ms) SELECT `cards`.* FROM `cards` WHERE `cards`.`id` = 547 LIMIT 1 =&gt; #&lt;Card id: 547, name: "Duplicates appearing"&gt; 1.9.2-p290 :010 &gt; c.notes.count (0.3ms) SELECT COUNT(*) FROM `notes` INNER JOIN `timeline_items` ON `notes`.`id` = `timeline_items`.`item_id` WHERE `timeline_items`.`card_id` = 547 AND `timeline_items`.`item_type` = 'Note' =&gt; 4 1.9.2-p290 :011 &gt; c.notes.last.card Note Load (2.7ms) SELECT `notes`.* FROM `notes` INNER JOIN `timeline_items` ON `notes`.`id` = `timeline_items`.`item_id` WHERE `timeline_items`.`card_id` = 547 AND `timeline_items`.`item_type` = 'Note' ORDER BY updated_at ASC LIMIT 1 Card Load (3.2ms) SELECT `cards`.* FROM `cards` INNER JOIN `timeline_items` ON `cards`.`id` = `timeline_items`.`card_id` WHERE `timeline_items`.`item_id` = 620 AND `timeline_items`.`item_type` = 'Note' LIMIT 1 =&gt; #&lt;Card id: 547, name: "Duplicates appearing"&gt; 1.9.2-p290 :013 &gt; c.notes &lt;&lt; Note.new(:content =&gt; 'Holee Sheeet Dawg', :user_id =&gt; 1) (0.2ms) BEGIN SQL (0.6ms) INSERT INTO `notes` (`content`, `created_at`, `updated_at`, `user_id`) VALUES ('Holee Sheeet Dawg', '2012-09-07 11:38:55', '2012-09-07 11:38:55', 1) (0.1ms) COMMIT (0.1ms) BEGIN SQL (0.3ms) INSERT INTO `timeline_items` (`card_id`, `created_at`, `item_id`, `item_type`, `updated_at`) VALUES (547, '2012-09-07 11:38:55', 625, 'Note', '2012-09-07 11:38:55') (0.5ms) COMMIT Note Load (1.8ms) SELECT `notes`.* FROM `notes` INNER JOIN `timeline_items` ON `notes`.`id` = `timeline_items`.`item_id` WHERE `timeline_items`.`card_id` = 547 AND `timeline_items`.`item_type` = 'Note' ORDER BY updated_at DESC =&gt; [#&lt;Note id: 625, content: "Holee Sheeet Dawg", user_id: 1, created_at: "2012-09-07 11:38:55", updated_at: "2012-09-07 11:38:55"&gt;, .....] 1.9.2-p290 :014 &gt; c.notes.count (0.7ms) SELECT COUNT(*) FROM `notes` INNER JOIN `timeline_items` ON `notes`.`id` = `timeline_items`.`item_id` WHERE `timeline_items`.`card_id` = 547 AND `timeline_items`.`item_type` = 'Note' =&gt; 5 </code></pre> <p><strong>EDIT:</strong></p> <p>I just noticed that my requirement of having <em>card.timeline</em> wasn't met yet. Because the join is coming from multiple tables, I wasn't able to get AR to handle the join for me (ideally *card.timeline_items.join(:notes, :attachments)* would have done the trick).</p> <p>To solve this, I added the following method to my card class:</p> <pre><code> def timeline (self.notes + self.attachments).sort { |a,b| a.updated_at &lt;=&gt; b.updated_at } end </code></pre>
 

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