Sha256: a264acf370fc4bb07e075de8fff96b11bdc5a8e53083555aa3eea57a576ed808

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# ActiveRecordViews

ActiveRecordViews makes it easy to create and update PostgreSQL database views for ActiveRecord models.

Advantages over creating views manually in migrations include:

* Automatic reloading in development mode.
  This avoids the need to to run `rake db:migrate:redo` after every change.

* Keeps view changes in a single SQL file instead of spread across multiple migration files.
  This allows changes to views to be easily reviewed with `git diff`.

## Installation

Add this line to your application's `Gemfile`:

```
gem 'activerecord_views'
```

## Usage

app/models/account.rb:

``` ruby
class Account < ActiveRecord::Base
  has_many :transactions

  has_one :account_balance
  delegate :balance, :to => :account_balance
end
```

app/models/transaction.rb:

``` ruby
class Transaction < ActiveRecord::Base
  belongs_to :account
end
```

app/models/account_balance.rb:

``` ruby
class AccountBalance < ActiveRecord::Base
  is_view

  belongs_to :account
end
```

app/models/account_balance.sql:

``` sql
SELECT accounts.id AS account_id, coalesce(sum(transactions.amount), 0) AS balance
FROM accounts
LEFT JOIN transactions ON accounts.id = transactions.account_id
GROUP BY accounts.id
```

Example usage:

``` ruby
p Account.first.balance

Account.includes(:account_balance).find_each do |account|
  p account.balance
end
```

### Usage outside of Rails

``` ruby
require 'active_record'
require 'active_record_views'
ActiveRecordViews.load_path = ['.']
ActiveRecordViews.init!
```

## License

MIT

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_views-0.0.1 README.markdown