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