---
title: "Rails polymorphic的使用和理解"
author: "夜鸣猪"
date: "2012-09-07T02:03:58Z"
tags: []
categories: ["编程语言"]
slug: "rails-polymorphic"
draft: false
lastmod: "2012-09-07T02:03:58Z"
id: 1673198
url: "https://hlee.iteye.com/blog/1673198"
---

```ruby
class Person < ActiveRecord::Base
  has_one :address, :as => :addressable
end

class Company < ActiveRecord::Base
  has_one :address, :as => :addressable
end

class Address < ActiveRecord::Base
 belongs_to :addressable, :polymorphic => true
end
```


Address 有adressable_id addressable_type其中，addressable_type可以为Company或者Person