Giter Club home page Giter Club logo

activerecord-virtual-columns's Introduction

Activerecord::VirtualColumns

Use SQL subqueries to eliminate N+1 queries and take advantage of existing model relations.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-virtual-columns'

Usage

class User < ActiveRecord::Base
  include ActiveRecord::VirtualColumns
  has_many :tweets
  virtual_column :tweet_count,
    scope: -> { Tweet.where('user_id = users.id').select('COUNT(*)') },
    method: -> { tweets.count }
end

class Tweet < ActiveRecord::Base
  belongs_to :user
end

# SELECT *, (SELECT COUNT(*) FROM tweets WHERE user_id = users.id) AS tweet_count
user = User.with_vcolumns(:tweet_count).find 1
# loads from memory (does not run query)
user.tweet_count

Transform values from SQL

You may want to select raw SQL values that you then transform in Ruby. For example, JSON:

virtual_column :uses_json,
  scope: -> { "'{ \"a\": 1 }'::json" },
  transform: ->(json_string) { JSON.load json_string }

Enforce the use of the scope

If no method is provided, MethodNotImplementedError will be raised, thus forcing users to preload using the scope form.

Gotchas

The methods in ActiveRecord::Calculations do not function as lazy scopes, rather they immediately execute a query. Because of this, if you want to make a calculation as part of your virtual column, use a raw SQL snippet:

# .maximum(:value) won't work here!
virtual_column :best_score,
  scope: -> { Score.where('user_id = users.id').select('MAX(value)') }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rigoleto/activerecord-virtual-columns.

Please add tests.

License

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

activerecord-virtual-columns's People

Contributors

reid-rigo avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.