模型属性包含对象的集合

时间:2013-06-10 17:35:14

标签: ruby-on-rails ruby mongodb hstore

我有主模型页面,它是容器。 该页面可以包含一些待办事项列表,备注,文件和讨论。我们的想法是让它们按特殊顺序排列。

Page.last.container # [Todolist_obj, Note_obj, File_obj, Note_obj, Discussion_obj, File_obj, File_obj] 
  • 所以我开始使用Mongodb

  • 或者我也考虑过将Postgres与hstore一起使用,但不知道它是否会有所帮助

  • 或者可能只是任何数据库并在获取页面时反序列化所有对象,并在保存时序列化对象

  • 或者我可以使用MTI创建超类Item并从中继承所有包含对象,并使Page有很多关系。

所以我不知道哪种方式最好?

或者有更好的方法?

1 个答案:

答案 0 :(得分:1)

我已经使用acts_as_list非常成功地实现了可排序对象。另外,我会将页面元素抽象为一个单独的模型,这里称为PageElement

我认为没有必要切换到NoSQL数据库(虽然我没有反对这种方法)。这是我正在思考的草图:

class Page < ActiveRecord::Base
  has_many :page_elements, :order => 'position'
  has_many :todo_lists,  :through => :page_elements, :source => :element, :source_type => 'TodoList'
  has_many :notes,       :through => :page_elements, :source => :element, :source_type => 'Note'
  has_many :files,       :through => :page_elements, :source => :element, :source_type => 'File'
  has_many :discussions, :through => :page_elements, :source => :element, :source_type => 'Discussion'
end

class PageElement < ActiveRecord::Base
  belongs_to :page
  belongs_to :element, :polymorphic => true
  acts_as_list :scope => :page
end

class TodoList < ActiveRecord::Base
  has_one :page_element, :as => :element
  has_one :page, :through => :page_elements 
end

class Note < ActiveRecord::Base
  has_one :page_element, :as => :element
  has_one :page, :through => :page_elements 
end

class File < ActiveRecord::Base
  has_one :page_element, :as => :element
  has_one :page, :through => :page_elements 
end

class Discussion < ActiveRecord::Base
  has_one :page_element, :as => :element
  has_one :page, :through => :page_elements 
end
相关问题