Skip to content

Commit

Permalink
how to use SQL to convert string-delimited arrays
Browse files Browse the repository at this point in the history
closes #60
  • Loading branch information
turadg committed Feb 16, 2013
1 parent 1167b57 commit 744d71e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.