【问题标题】:STI and polymorphsSTI 和多晶型物
【发布时间】:2010-04-08 21:31:20
【问题描述】:

我的代码有问题

class Post < ActiveRecord::Base
end

class NewsArticle < Post
  has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at'
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true, :counter_cache => true
end

并且在尝试去获取一些新闻文章的 cmets 我在日志中看到的类似

  Comment Load (0.9ms)   SELECT "comments".* FROM "comments" WHERE ("comments"."commentable_id" = 1 and "comments"."commentable_type" = 'Post') ORDER BY created_at

奇怪的是 "commentable_type" = 'Post'。 怎么了?

PS:Rails 2.3.5 && ruby​​ 1.8.7(2010-01-10 补丁级别 249)[i686-darwin10]

【问题讨论】:

    标签: ruby-on-rails activerecord polymorphic-associations single-table-inheritance


    【解决方案1】:

    commentable_type字段需要存储包含数据的表名,一旦从右表加载了该行,继承的类型将从type加载帖子表中的strong>列。

    所以:

    这里的注释指向它所在的表。帖子表,id 1

    >> Comment.first
    => #<Comment id: 1, commentable_id: 1, commentable_type: "Post", body: "test", created_at: "2010-04-09 00:56:36", updated_at: "2010-04-09 00:56:36">
    

    然后加载NewsArticle,从posts中加载id 1,这里的type表示NewsArticle。

    >> Comment.first.commentable
    => #<NewsArticle id: 1, type: "NewsArticle", name: "one", body: "body", created_at: "2010-04-09 00:55:35", updated_at: "2010-04-09 00:55:35">
    >> Comment.first.commentable.class.table_name
    => "posts"
    

    如果 commentable_type 持有"NewsArticle",则必须查看类以确定表。这样一来,它就可以只查看表格并担心到达那里的类型。

    【讨论】:

    • 对于多态关系,只有表重要,一旦加载了表,就可以确定继承类型。
    【解决方案2】:

    查看ActiveRecord::Associations API 的多态关联部分。有一点关于将多态关联与单表继承结合使用。按照该部分中的第二个代码示例,我认为这可能接近您想要的

    class Comment < ActiveRecord::Base
      belongs_to :commentable, :polymorphic => true, :counter_cache => true
    
      def commentable_type=(sType)
       super(sType.to_s.classify.constantize.base_class.to_s)
      end
    end
    
    class Post < ActiveRecord::Base
      has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at'
    end
    
    class NewsArticle < Post
    end
    

    【讨论】:

      【解决方案3】:

      def commentable_type=(sType) 超级(sType.to_s.classify.constantize.base_class.to_s) 结束

      该方法返回类为Post,如果要将继承的类Post存储为commentable_type怎么办?

      【讨论】:

        【解决方案4】:

        好问题。我在使用 Rails 3.1 时遇到了完全相同的问题。看来问题还没有解决。显然,在 Rails 中结合使用多态关联和单表继承 (STI) 有点复杂。

        Rails 3.2 的当前 Rails 文档给出了合并 polymorphic associations and STI 的建议:

        结合单表使用多态关联 继承(STI)有点棘手。为了让协会 按预期工作,确保存储 STI 的基本模型 多态关联的类型列中的模型。

        在您的情况下,基本模型将是“Post”,即所有 cmets 的“commentable_type”应该是“Post”。

        【讨论】:

          【解决方案5】:

          从技术上讲,这实际上没有任何问题。当 Rails 处理多态关联并且关联的对象使用 STI 时,它只是使用基类作为类型(在您的情况下为“commentable_type”)。

          如果您将 Post 和 NewsArticle 放在不同的表中,显然 commentable_type 将分别显示为 Post 和 NewsArticle。

          【讨论】:

          • 如果正确,您如何从 NewsArticle 中检索 cmets?我遇到了类似的问题,我的查询没有返回任何内容,因为它正在搜索基类类型。
          猜你喜欢
          • 2013-03-07
          • 1970-01-01
          • 1970-01-01
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多