You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ Rails' polymorphic associations are pretty useful. The example they give to set
6
6
classPicture < ActiveRecord::Base
7
7
belongs_to :imageable, polymorphic:true
8
8
end
9
-
9
+
10
10
classEmployee < ActiveRecord::Base
11
11
has_many :pictures, as::imageable
12
12
end
13
-
13
+
14
14
classProduct < ActiveRecord::Base
15
15
has_many :pictures, as::imageable
16
16
end
@@ -31,7 +31,7 @@ class CreatePictures < ActiveRecord::Migration
31
31
end
32
32
```
33
33
34
-
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
34
+
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
35
35
36
36
## Installation
37
37
@@ -60,7 +60,7 @@ class Picture < ActiveRecord::Base
Next, include `PolymorphicIntegerType::Extensions` into any of the models that point back to the polymorphic integer type association (e.g., `Picture#imageable`) and add a [polymorphic association using `as:`](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations).
65
65
66
66
```ruby
@@ -69,7 +69,7 @@ class Employee < ActiveRecord::Base
69
69
70
70
has_many :pictures, as::imageable
71
71
end
72
-
72
+
73
73
classProduct < ActiveRecord::Base
74
74
includePolymorphicIntegerType::Extensions
75
75
@@ -84,25 +84,25 @@ You can also store polymorphic type mappings separate from your models. This sho
84
84
```ruby
85
85
PolymorphicIntegerType::Mapping.configuration do |config|
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
90
+
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
91
91
92
92
### Migrating an existing association
93
93
94
94
If you want to convert a polymorphic association that is already a string, you'll need to set up a migration. (Assuming SQL for the time being, but this should be pretty straightforward.)
0 commit comments