rails 多态

Polymorphic Associations

多型关连(Polymorphic Associations)可以让一个Model不一定关连到某一个特定的Model,秘诀在于除了整数的_id外部键之外,再加一个字串的_type栏位说明是哪一种Model

例如一个Comment model,我们可以透过多型关连让它belongs_to到各种不同的Model上,假设我们已经有了ArticlePhoto这两个Model,然后我们希望这两个Model都可以被留言。不用多型关连的话,你得分别建立ArticleCommentPhotoCommentmodel用多型关连的话,无论有多少种需要被留言的Model,只需要一个Comment model即可:

rails g model comment content:text commentable_id:integer commentable_type

这样会产生下面的Migration 档案:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :content
      t.integer :commentable_id
      t.string :commentable_type

      t.timestamps
    end
  end
end

这个Migration档案中,我们用content这个栏位来储存留言的内容,commentable_id用来储存被留言的物件的idcommentable_type则用来储存被留言物件的种类,以这个例子来说被留言的对象就是ArticlePhoto这两种Model,这个Migration档案也可以改写成下面这样:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :content
      t.belongs_to :commentable, :polymorphic => true

      t.timestamps
    end
  end
end

回到我们的Model,我们必须指定他们的关联关系:

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

class Article < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Photo < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

这样会告诉Rails如何去设定你的多型关系,现在让我们进console实验看看:

article = Article.first

# 透过关连新增留言
comment = article.comments.create(:content => "First Comment")

# 你可以发现Rails 很聪明的帮我们指定了被留言物件的种类和id
comment.commentable_type => "Article"
comment.commentable_id => 1

# 也可以透过commentable 反向回查关连的物件
comment.commentable => #<Article id: 1, ....>

DBA背景的同学可能会注意到PolymorphicAassociations无法做到保证Referential integrity特性。原因很简单,既然不知道_id会指到哪个table,自然也就没办法在资料库层级加上Foreign key constraint



转自:http://ihower.tw/rails3/activerecord-others.html#transactions

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值