Skip to content

A simple query interface for pulling deeply nested data from records.

License

Notifications You must be signed in to change notification settings

delonnewman/activerecord-pull

Repository files navigation

Ruby

ActiveRecord::Pull::Alpha

A simple query interface for pulling deeply nested data from records.

Synopsis

  class Survey < ActiveRecord::Base
    has_many :questions
    has_many :responses
  end

  class Question < ActiveRecord::Base
    has_many :answers
  end

  class Answer < ActiveRecord::Base
    belongs_to :reply
    belongs_to :question
  end

  class Response < ActiveRecord::Base
    belongs_to :survey
    has_many :answers
  end

  Survey.find(34).pull(:name, questions: [:text, answers: [:created_at]])
  Response.where('created_at < ?', Date.new(2018, 7, 5)).pull({ survey: :name }, { answers: :value }) 

Why?

When using an SQL database modeling trees and ontologies is an exercise in relational database design. Unfortunately, when dealing with deeply nested and overlapping data (e.g. medical records and social graphs) trees and ontologies won't do. We need to model graphs, a much more sparsely structured data model, but this leads to very complex (or sparse) table structures and very complex SQL queries.

Alternatives to SQL databases like Datomic, RDF and graph databases resolve this problem in the general case, but sometimes those solutions are not a viable option for one reason or another. This gem attempts to provide some of the convenience of these solutions as an extension to ActiveRecord. As a bonus it makes querying trees and ontologies simple too!

Pull Syntax

The pull syntax is inspired by the pull syntax that is used in Datomic.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-pull-alpha'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-pull-alpha

See Also

License

The gem is available as open source under the terms of the MIT License.