Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:46+00:00 2026-06-11T12:38:46+00:00

I’d like to create a ‘timeline’ feature for an existing Card model. The Card

  • 0

I’d like to create a ‘timeline’ feature for an existing Card model. The Card already has_many Notes and has_many Attachments. I’d like to be able to:

  • access notes, attachments (and other models eventually) in a unified collection with a nice method like: card.timeline
  • still be able to access a card’s notes and attachments like: card.notes
  • still be able to access a note’s parent card like: note.card
  • be able to add items to the card’s timeline, with an API like: card.timeline << note

I think I have my DB set up correctly, it’s the association declaration I can’t seem to get right. Here’s my schema:

  create_table "cards", :force => true do |t|
    t.string   "name"
  end

  create_table "timeline_items", :force => true do |t|
    t.integer  "card_id",    :null => false # FK from cards table
    t.integer  "item_id",    :null => false # FK from notes or attachments table
    t.string   "item_type",  :null => false # either 'Note' or 'Attachment'
  end

  create_table "notes", :force => true do |t|
    t.text     "content"
  end

  create_table "attachments", :force => true do |t|
    t.string   "file_file_name"
  end

Anyone know how I can achieve this using ActiveRecord? It’s driving me fudging mental!

A starting point is:

class Card < ActiveRecord::Base
  has_many    :timeline_items
  has_many    :notes,       :through => :timeline_items, :source => :item, :source_type => 'Note', :order => 'updated_at DESC'
  has_many    :attachments, :through => :timeline_items, :source => :item, :source_type => 'Attachment', :order => 'updated_at DESC'
end

class TimelineItem < ActiveRecord::Base
  belongs_to :card
  belongs_to :item, :polymorphic => true
end

class Note < ActiveRecord::Base
  has_one     :card, :through => :timeline_items
  has_one     :timeline_item, :as => :item
end

Thanks in advance
~Stu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:38:47+00:00Added an answer on June 11, 2026 at 12:38 pm

    Okay – after struggling on and off with this, I crack it within 10mins of posting to stackoverflow! Typical.

    To save others from banging their heads against walls, here’s what I had wrong:

    Note should have been:

    class Note < ActiveRecord::Base
      has_one     :card, :through => :timeline_item #not timeline_items
      has_one     :timeline_item, :as => :item
    end
    

    And that was it! I was trying to use the creation methods used in this article, but actually that’s not required.

    Here’s the console output, showing that the sql statements are all using the timeline_items table:

    1.9.2-p290 :009 > c = Card.find(547)
      Card Load (0.3ms)  SELECT `cards`.* FROM `cards` WHERE `cards`.`id` = 547 LIMIT 1
     => #<Card id: 547, name: "Duplicates appearing"> 
    
    1.9.2-p290 :010 > 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'
     => 4 
    
    1.9.2-p290 :011 > 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
     => #<Card id: 547, name: "Duplicates appearing"> 
    
    1.9.2-p290 :013 > c.notes << Note.new(:content => 'Holee Sheeet Dawg', :user_id => 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
     => [#<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">, .....]
    
    1.9.2-p290 :014 > 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'
     => 5
    

    EDIT:

    I just noticed that my requirement of having card.timeline 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).

    To solve this, I added the following method to my card class:

      def timeline
        (self.notes + self.attachments).sort { |a,b| a.updated_at <=> b.updated_at }
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.