<commit_before>#Goodbye serialize, hello hstore.

[![Build Status](https://secure.travis-ci.org/diogob/activerecord-postgres-hstore.svg?branch=master)](http://travis-ci.org/diogob/activerecord-postgres-hstore)
[![Code Climate](https://codeclimate.com/github/diogob/activerecord-postgres-hstore.svg)](https://codeclimate.com/github/diogob/activerecord-postgres-hstore)

You need dynamic columns in your tables. What do you do?

* Create lots of tables to handle it. Nice, now you’ll need more models and lots of additional sqls. Insertion and selection will be slow as hell.
* Use a noSQL database just for this issue. Good luck.
* Create a serialized column. Nice, insertion will be fine, and reading data from a record too. But, what if you have a condition in your select that includes serialized data? Yeah, regular expressions.

##Common use cases

Add settings to users, like in rails-settings or HasEasy.

```ruby
class User < ActiveRecord::Base
  serialize :settings, ActiveRecord::Coders::Hstore
end
user = User.create settings: {theme: 'navy'}
user.settings['theme']
```

##Note about Rails 4

If you are using Rails 4 you don't need this gem as ActiveRecord 4 provides HStore type support out of the box.

You can test it with a migration like this:
```ruby
class CreateTest < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.hstore :data
    end
  end
end
```

Its model:
```ruby
class Test < ActiveRecord::Base
end
```

Then you can use the hash field straight away:
```ruby
irb(main):003:0> t = Test.new data: {a: 1, b:2}
=> #<Test id: nil, data: {"a"=>"1", "b"=>"2"}>
irb(main):004:0> t.save!
   (0.3ms)  BEGIN
  SQL (2.3ms)  INSERT INTO "tests" ("data") VALUES ($1) RETURNING "id"  [["data", "\"a\"=>\"1\",\"b\"=>\"2\""]]
   (0.5ms)  COMMIT
=> true
irb(main):005:0> t
=> #<Test id: 1, data: {"a"=>"1", "b"=>"2"}>
irb(main):006:0> t.data
=> {"a"=>"1", "b"=>"2"}
irb(main):007:0> t.data['a']
=> "1"
```

For more information take a look [here](http://jes.al/2013/11/using-postgres-hstore-rails4/)

##Note about 0.7
<commit_msg>Docs suggestion from the previously confused<commit_after>#Goodbye serialize, hello hstore.

[![Build Status](https://secure.travis-ci.org/diogob/activerecord-postgres-hstore.svg?branch=master)](http://travis-ci.org/diogob/activerecord-postgres-hstore)
[![Code Climate](https://codeclimate.com/github/diogob/activerecord-postgres-hstore.svg)](https://codeclimate.com/github/diogob/activerecord-postgres-hstore)

You need dynamic columns in your tables. What do you do?

* Create lots of tables to handle it. Nice, now you’ll need more models and lots of additional sqls. Insertion and selection will be slow as hell.
* Use a noSQL database just for this issue. Good luck.
* Create a serialized column. Nice, insertion will be fine, and reading data from a record too. But, what if you have a condition in your select that includes serialized data? Yeah, regular expressions.

##Common use cases

Add settings to users, like in rails-settings or HasEasy.

```ruby
class User < ActiveRecord::Base
  serialize :settings, ActiveRecord::Coders::Hstore
end
user = User.create settings: {theme: 'navy'}
user.settings['theme']
```

##Note about Rails 4

If you are using Rails 4 you don't need this gem as ActiveRecord 4 provides HStore type support out of the box.  ActiveRecord will see your HStore column and do all of the work for you.  **Additional code is no longer needed.**

You can test it with a migration like this:
```ruby
class CreateTest < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.hstore :data
    end
  end
end
```

Its model:
```ruby
class Test < ActiveRecord::
  # before Rails 4, we'd have to this here:
  # serialize :data, ActiveRecord::Coders::Hstore
end
```

Then you can use the hash field straight away:
```ruby
irb(main):003:0> t = Test.new data: {a: 1, b:2}
=> #<Test id: nil, data: {"a"=>"1", "b"=>"2"}>
irb(main):004:0> t.save!
   (0.3ms)  BEGIN
  SQL (2.3ms)  INSERT INTO "tests" ("data") VALUES ($1) RETURNING "id"  [["data", "\"a\"=>\"1\",\"b\"=>\"2\""]]
   (0.5ms)  COMMIT
=> true
irb(main):005:0> t
=> #<Test id: 1, data: {"a"=>"1", "b"=>"2"}>
irb(main):006:0> t.data
=> {"a"=>"1", "b"=>"2"}
irb(main):007:0> t.data['a']
=> "1"
```

For more information take a look [here](http://jes.al/2013/11/using-postgres-hstore-rails4/)

##Note about 0.7

{'fraction_non_alphanumeric': 0.15579958819492107, 'fraction_numerical': 0.023564401738732556, 'mean_word_length': 4.4581772784019975, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 43, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1904700', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1495, 'n_words': 512}