Friday, 29 June 2012

postgres_ext gem native postgres datatypes in rails

Hi,

We are using different datatypes depending on the requirement of data that is being saved in database.

Coming to postgres database, it supports many datatypes and we can't use all those datatypes directly in rails.

To over come this problem, we had a new gem postgres_ext


postgres_ext supports 3.2 and above version of rails.

postgres_ext adds migration and schema.rb support for the following PostgresSQL type:

    INET
    CIDR
    MACADDR
    UUID
    Arrays


example:

create_table :users do |t|
  t.inet :myip


  t.cidr :mysubip


  t.macaddr :ip_address


  t.uuid :member_id

  t.integer :friend_ids, :array => true

end


postgres_ext converts INET and CIDR values to IPAddr instances.

example:

create_table :inet_examples do |t|
  t.inet :ip_address
end

class MyExample < ActiveRecord::Base
end

my_example = MyExample.new
my_example.ip_address = '127.0.0.0/34'
my_example.ip_address
# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>
my_example.save

my_example_u = MyExample.first
my_example_u.ip_address

# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>



array type:

example;

create_table :people do |t|
  t.integer :favorite_numbers, :array => true
end

class User < ActiveRecord::Base
end

user = User.new
user.like_numbers = [1,2,3]
user.flike_numbers
# => [1,2,3]
user.save

user_2 = user.first
user_2.like_numbers
# => [1,2,3]
user_2.like_numbers.first.class
# => Fixnum



here is the git url: https://github.com/umamahesh/pg_array_parser

Thank You,
Uma Mahesh.

No comments:

Post a Comment