2012-09-07 42 views
1

我想爲現有的Card模型創建'時間線'功能。卡已has_many註釋和has_many附件。我希望能夠:代表現有has_many模型的Rails多態關聯

  • 訪問筆記,附件(等車型最終)在一個統一的收集與像一個不錯的方法:card.timeline
  • 仍然能夠訪問卡註釋和附件,如:card.notes
  • 仍然能夠訪問記的父卡,如:note.card
  • 能夠將項目添加到該卡的時間表,與API,如:卡。時間軸< <注

我想我有我的數據庫設置正確,這是該協會的聲明,我似乎無法得到正確的。這是我的模式:

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 

任何人都知道我可以如何使用ActiveRecord實現這一目標?這讓我陷入心理上的困擾!

一個出發點是:

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 

在此先感謝 〜斯圖

回答

0

好 - 掙扎後和關閉此,我破解它張貼到計算器的10分鐘之內!典型。

挽救他人從撞牆撞頭,這裏是我有錯:

注意應該是:

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

,就是這樣!我試圖使用this article中使用的創建方法,但實際上這不是必需的。

這裏是控制檯輸出,顯示出SQL語句都使用timeline_items表:

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 

編輯:

我只注意到我有card.timeline的要求 WASN還沒有見過。因爲連接來自多個表,所以我無法讓AR爲我處理連接(理想情況下* card.timeline_items.join(:notes,:attachments)*本來就是這樣做的)。

爲了解決這個問題,我添加了下面的方法來我的卡類:

def timeline 
    (self.notes + self.attachments).sort { |a,b| a.updated_at <=> b.updated_at } 
    end 
+0

我想我已經回答了90%的我自己的問題就在這裏:)感謝張貼您的解決方案。 –

+0

沒問題 - 如果我錯過了任何事情,請告訴我 – Stu

相關問題