模型属性包含对象的集合 [英] Model attribute contains collection of object

查看:54
本文介绍了模型属性包含对象的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有主模型页面,它是容器.该页面可以有一些待办事项列表、笔记、文件和讨论.这个想法是让它们按特殊顺序排列.

Page.last.container # [Todolist_obj, Note_obj, File_obj, Note_obj, Discussion_obj, File_obj, File_obj] 

  • 所以我开始接触使用Mongodb

    • So I came to approach to use Mongodb

      或者我也想过在hstore上使用Postgres,但不知道它会不会有帮助

      Or I also thought about using Postgres with hstore, but don't know will it help or not

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

      Or maybe just any database and deserialize all objects when getting page, and serialize objects when saving

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

      Or I can make superclass Item and inherit all containing objects from it using MTI and make Page has many relationship.

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

      或者也许有更好的方法?

      推荐答案

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

      I have used acts_as_list for implementing sortable objects very successfully. Additionally, i would abstract the elements of a page into a separate model, here called PageElement.

      我认为没有必要切换到 NoSQL 数据库(尽管我不反对这种方法).这是我的想法的粗略草图:

      I think there is no need to switch to a NoSQL database (although i have nothing against this approach). Here is a rough sketch of what i'm thinking:

      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
      

      这篇关于模型属性包含对象的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆