Skip to content

Commit 5a73ed9

Browse files
authored
Merge pull request #32 from Drew-Goddyn/update-readme
update reference to reminders instead of pictures
2 parents fa3a809 + 2554e1f commit 5a73ed9

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Rails' polymorphic associations are pretty useful. The example they give to set
66
class Picture < ActiveRecord::Base
77
belongs_to :imageable, polymorphic: true
88
end
9-
9+
1010
class Employee < ActiveRecord::Base
1111
has_many :pictures, as: :imageable
1212
end
13-
13+
1414
class Product < ActiveRecord::Base
1515
has_many :pictures, as: :imageable
1616
end
@@ -31,7 +31,7 @@ class CreatePictures < ActiveRecord::Migration
3131
end
3232
```
3333

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.
3535

3636
## Installation
3737

@@ -60,7 +60,7 @@ class Picture < ActiveRecord::Base
6060
belongs_to :imageable, polymorphic: {1 => "Employee", 2 => "Product"}
6161
end
6262
```
63-
63+
6464
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).
6565

6666
```ruby
@@ -69,7 +69,7 @@ class Employee < ActiveRecord::Base
6969

7070
has_many :pictures, as: :imageable
7171
end
72-
72+
7373
class Product < ActiveRecord::Base
7474
include PolymorphicIntegerType::Extensions
7575

@@ -84,25 +84,25 @@ You can also store polymorphic type mappings separate from your models. This sho
8484
```ruby
8585
PolymorphicIntegerType::Mapping.configuration do |config|
8686
config.add :imageable, {1 => "Employee", 2 => "Product" }
87-
end
87+
end
8888
```
8989

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.
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.
9191

9292
### Migrating an existing association
9393

9494
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.)
9595

9696
```ruby
9797
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
98-
98+
9999
def up
100100
change_table :pictures do |t|
101101
t.integer :new_imageable_type
102102
end
103103

104104
execute <<-SQL
105-
UPDATE reminders
105+
UPDATE picture
106106
SET new_imageable_type = CASE imageable_type
107107
WHEN 'Employee' THEN 1
108108
WHEN 'Product' THEN 2

0 commit comments

Comments
 (0)