Skip to content

Maps attributes to hstore columns in postgres databases for easy casting and validations.

License

Notifications You must be signed in to change notification settings

defsprite/hstore-attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HstoreAttributes

Build Status Code Climate

HstoreAttributes is a small extension on top of activerecord-postgres-hstore, which creates "fake" attributes that map into the hstore hash. By defining these attributes you get automatic validations and type casting. The approach is very similar to the ActiveRecord::Store feature, yielding better query and index performance in comparison to simple hash-to-yml-string usage.

A small example:

class Product < ActiveRecord::Base

  # serialize as hstore (not needed for activerecord-postgres-hstore < 0.7)
  serialize :properties, ActiveRecord::Coders::Hstore

  # define attributes with types to be mapped into the hstore
  hstore :properties, :accessors => { :width => :integer, :height => :integer, :color => :string }

  # if you want to dump strings only:
  hstore :properties, :accessors => [:color, :other_property, :third_property]

  validates_numericality_of :weight

  #  ...
end

Installation

Add this line to your application's Gemfile:

gem 'hstore-attributes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hstore-attributes

Usage

Usage should be pretty straightforward as shown in the example. Be sure to create a hstore column that takes the mapped attributes. Please note that this works for postgresql databases only.

See: https://github.com/softa/activerecord-postgres-hstore

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Maps attributes to hstore columns in postgres databases for easy casting and validations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages