Polymorphic Associations
多型关连(Polymorphic Associations)可以让一个Model不一定关连到某一个特定的Model,秘诀在于除了整数的_id
外部键之外,再加一个字串的_type
栏位说明是哪一种Model。
例如一个Comment model
,我们可以透过多型关连让它belongs_to
到各种不同的Model上,假设我们已经有了Article与Photo这两个Model,然后我们希望这两个Model都可以被留言。不用多型关连的话,你得分别建立ArticleComment和PhotoComment的model。用多型关连的话,无论有多少种需要被留言的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用来储存被留言的物件的id而commentable_type则用来储存被留言物件的种类,以这个例子来说被留言的对象就是Article与Photo这两种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, ....>