Giter Site home page Giter Site logo

envoy / activerecord_pg_stuff Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dmexe/activerecord_pg_stuff

0.0 3.0 0.0 19 KB

Adds support for working with temporary tables and pivot tables (PostgreSQL only)

License: MIT License

Ruby 100.00%

activerecord_pg_stuff's Introduction

ActiveRecordPgStuff

Adds support for working with temporary tables and pivot tables (PostgreSQL only).

  • Build Status

Installation

Add this line to your application's Gemfile:

gem 'activerecord_pg_stuff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord_pg_stuff

Usage

Temporary tables

Example:

User.select(:id, :email).temporary_table do |rel|
  rel.pluck(:id)
end

Pivot tables

Before using, you need to create the extension:

CREATE EXTENSION tablefunc

Example:

CREATE TABLE payments (id integer, amount integer, seller_id integer, created_at timestamp)
INSERT INTO payments
  VALUES (1, 1,  1, '2012-10-12 10:00 UTC'),
         (2, 3,  1, '2012-11-12 10:00 UTC'),
         (3, 5,  2, '2012-09-12 10:00 UTC'),
         (4, 7,  2, '2012-10-12 10:00 UTC'),
         (5, 11, 2, '2012-11-12 10:00 UTC'),
         (6, 13, 2, '2012-11-12 10:00 UTC')

Payment
  .select("SUM(amount) AS amount", :seller_id, "DATE_TRUNC('month', created_at) AS month")
  .group("seller_id, DATE_TRUNC('month', created_at)")
  .temporary_table do |rel|
    # :month     - for row
    # :seller_id - for column
    # :amount    - for value
    rel.pivot(:month, :seller_id, :amount)
end

expect(result.headers).to eq [nil, 1, 2]

expect(result.rows).to eq [
  [ Time.utc(2012, 9,  1), nil, 5  ],
  [ Time.utc(2012, 10, 1), 1,   7  ],
  [ Time.utc(2012, 11, 1), 3,   24 ],
]

Contributing

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.