2012-09-07 98 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

相关问题