Permalink
Browse files

how to use SQL to convert string-delimited arrays

closes #60
  • Loading branch information...
turadg committed Feb 16, 2013
1 parent 1167b57 commit 744d71e2f352c368a208f0e90fb20363613f44ef
Showing with 16 additions and 0 deletions.
  1. +16 −0 docs/migrations.md
View
@@ -74,3 +74,19 @@ create_table :testing do |t|
# char varying(30)[]
end
```
+
+### Converting to Arrays
+
+If you have an existing column with a string-delimited array (e.g. 'val1 val2 val3') convert that data using SQL in your migration.
+
+```ruby
+class AddLinkedArticleIdsToLinkSet < ActiveRecord::Migration
+ def change
+ add_column :link_sets, :linked_article_ids, :integer, :array => true, :default => []
+ execute <<-eos
+ UPDATE link_sets
+ SET linked_article_ids = cast (string_to_array(linked_articles_string, ' ') as integer[])
+ eos
+ end
+end
+````

0 comments on commit 744d71e

Please sign in to comment.