| | 281 | # |
|---|
| | 282 | # Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order |
|---|
| | 283 | # for the associations to work as expected, ensure that you store the base model for the STI models in the |
|---|
| | 284 | # type column of the polymorphic association. To continue with the asset example above, suppose there are guest posts |
|---|
| | 285 | # and member posts that use the posts table for STI. So there will be an additional 'type' column in the posts table. |
|---|
| | 286 | # |
|---|
| | 287 | # class Asset < ActiveRecord::Base |
|---|
| | 288 | # belongs_to :attachable, :polymorphic => true |
|---|
| | 289 | # |
|---|
| | 290 | # def attachable_type=(sType) |
|---|
| | 291 | # super(sType.to_s.classify.constantize.base_class.to_s) |
|---|
| | 292 | # end |
|---|
| | 293 | # end |
|---|
| | 294 | # |
|---|
| | 295 | # class Post < ActiveRecord::Base |
|---|
| | 296 | # # because we store "Post" in attachable_type now :dependent => :destroy will work |
|---|
| | 297 | # has_many :assets, :as => :attachable, :dependent => :destroy |
|---|
| | 298 | # end |
|---|
| | 299 | # |
|---|
| | 300 | # class GuestPost < ActiveRecord::Base |
|---|
| | 301 | # end |
|---|
| | 302 | # |
|---|
| | 303 | # class MemberPost < ActiveRecord::Base |
|---|
| | 304 | # end |
|---|