No commit activity in last 3 years
No release in over 3 years
PostgreSQL allows to create a procedure which returns a result set. The gem modifies ActiveRecord so that it's possible to create (readonly) model based on resultset instead of a database table.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.6
~> 3.0

Runtime

~> 0.17
 Project Readme

ActiveRecord PostgreSQL procedures

PostgreSQL allows to create a procedure which returns a result set. The gem modifies ActiveRecord (>= 4.2) so that it's possible to create (readonly) model based on resultset instead of a database table.

Given a PostgreSQL procedure

CREATE FUNCTION books(whatever INT)
RETURNS TABLE (
    id INTEGER,
    title CHARACTER VARYING,
    volume INTEGER,
    published BOOLEAN
) LANGUAGE SQL STABLE
AS $$
...
$$

and model

class Book < ActiveRecord::Base
end

you can read the model the same way you read model based on database table. You need to call the procedure using the from clause.

Book.from("books(1)") # <ActiveRecord::Relation [#<Book id: 1, title: "Small John", volume: 7, published: true>, ...]>

Relationship and other stuff from Active Record also work. In fact the gem modifies PostgreSQL adapter, Active Record doesn't even know if it reads table or a resultset.

Notes

  • The first column is used as a primary key.
  • You can't write a model back to the database.

Download and installation

The latest version of Active Record PostgreSQL procedures can be installed with RubyGems:

  % [sudo] gem install activerecord_postgresql_procedures

Source code can be downloaded from GitHub:

License

The project is released under the MIT license:

Support

Bug reports can be filed here: