Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
natematykiewicz committed May 5, 2024
1 parent 5116d51 commit 250ddc9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions guides/source/association_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,16 @@ class CreatePictures < ActiveRecord::Migration[7.2]
end
```

In this example, the `imageable` can have any model assigned to it. If you wanted to ensure that the `imageable_type` is only `Employee` or `Product`, you can set the `polymorphic` option to an array of allowed class names. This will [add an inclusion validator][inclusion validator] to the `imageable_type` column, which adds a validation error if `imageable` is not one of the expected classes:

[inclusion validator]: active_record_validations.html#inclusion

```ruby
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: ["Employee", "Product"]
end
```

NOTE: Since polymorphic associations rely on storing class names in the
database, that data must remain synchronized with the class name used by the
Ruby code. When renaming a class, make sure to update the data in the
Expand Down

0 comments on commit 250ddc9

Please sign in to comment.