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
在此先感谢 〜斯图
我想我已经回答了90%的我自己的问题就在这里:)感谢张贴您的解决方案。 –
没问题 - 如果我错过了任何事情,请告诉我 – Stu