A simple query interface for pulling deeply nested data from records.
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 })
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!
The pull syntax is inspired by the pull syntax that is used in Datomic.
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
The gem is available as open source under the terms of the MIT License.