Adds support for missing PostgreSQL data types to ActiveRecord.
- Arel support for INET, CIDR and Array related where clauses
- Backport HStore code from Rails 4.0
Add this line to your application's Gemfile:
gem 'postgres_ext'
And then execute:
$ bundle
Or install it yourself as:
$ gem install postgres_ext
Just require 'postgres_ext'
and use ActiveRecord as you normally would! postgres_ext extends
ActiveRecord's data type handling.
create_table :testing do |t|
t.inet :inet_column
# or
t.inet :inet_column_1, :inet_column_2
# or
t.column :inet_column, :inet
end
create_table :testing do |t|
t.cidr :cidr_column
# or
t.cidr :cidr_column_1, :cidr_column_2
# or
t.column :cidr_column, :cidr
end
create_table :testing do |t|
t.macaddr :macaddr_column
# or
t.macaddr :macaddr_column_1, :macaddr_column_2
# or
t.column :macaddr_column, :macaddr
end
create_table :testing do |t|
t.uuid :uuid_column
# or
t.uuid :uuid_column_1, :uuid_column_2
# or
t.column :uuid_column, :uuid
end
Arrays are created from any ActiveRecord supported datatype (including ones added by postgre_ext), and respect length constraints
create_table :testing do |t|
t.integer :int_array, :array => true
# integer[]
t.integer :int_array, :array => true, :length => 2
# smallint[]
t.string :macaddr_column_1, :array => true, :length => 30
# char varying(30)[]
end
INET and CIDR values are converted to IPAddr objects when retrieved from the database, or set as a string.
create_table :inet_examples do |t|
t.inet :ip_address
end
class InetExample < ActiveRecord::Base
end
inetExample = InetExample.new
inetExample.ip_address = '127.0.0.0/24'
inetExample.ip_address
# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>
inetExample.save
inet_2 = InetExample.first
inet_2.ip_address
# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>
Array values can be set with Array objects. Any array stored in the database will be converted to a properly casted array of values on the way out.
create_table :people do |t|
t.integer :favorite_numbers, :array => true
end
class Person < ActiveRecord::Base
end
person = Person.new
person.favorite_numbers = [1,2,3]
person.favorite_numbers
# => [1,2,3]
person.save
person_2 = Person.first
person_2.favorite_numbers
# => [1,2,3]
person_2.favorite_numbers.first.class
# => Fixnum
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request