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
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,10 +22,12 @@ class CreateCompanies < ActiveRecord::Migration
22
22
end
23
23
```
24
24
25
-
The problem with this approach is that `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 `type` column.
25
+
The problem with this approach is that `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 `type` column.
26
26
27
27
## Installation
28
28
29
+
_Current versions of this gem (>= v0.2.0) only support Ruby 3+ and ActiveRecord >= v6.1. For Ruby <= v2.7 or ActiveRecord <= 6.0, use v0.1.3._
30
+
29
31
Add this line to your application's Gemfile:
30
32
31
33
gem 'inheritance_integer_type'
@@ -42,28 +44,28 @@ Or install it yourself as:
42
44
43
45
The gem is pretty straightforward to use.
44
46
45
-
First, set the `integer_inheritance` value on each of the subclasses.
47
+
First, set the `integer_inheritance` value on each of the subclasses.
46
48
```ruby
47
49
classFirm < Company
48
50
self.integer_inheritance =1
49
51
end
50
-
52
+
51
53
classClient < Company
52
54
self.integer_inheritance =2
53
55
end
54
-
56
+
55
57
classPriorityClient < Client
56
58
self.integer_inheritance =3
57
59
end
58
60
```
59
61
60
62
61
-
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 `PriorityFirm`, but forgot to include set the mapping, it would effectively get `to_i` called on it and stored in the database. `"Priority".to_i == 0`, so if your mapping included 0, this would create a weird bug.
63
+
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 `PriorityFirm`, but forgot to include set the mapping, it would effectively get `to_i` called on it and stored in the database. `"Priority".to_i == 0`, so if your mapping included 0, this would create a weird bug.
62
64
63
65
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