Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
Created October 3, 2020 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osyo-manga/d1434e8c694dbb510dc03b4a2b1abb1e to your computer and use it in GitHub Desktop.
Save osyo-manga/d1434e8c694dbb510dc03b4a2b1abb1e to your computer and use it in GitHub Desktop.
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# ここに依存する gem を記述する
gem "activerecord", "6.0.3"
# gem "rails", github: "rails/rails"
gem "sqlite3"
gem "ruby_jard"
# frozen_string_literal: true
require "stringio"
# require "ruby_jard"
temp_io = $stdout
$stdout = StringIO.new
require "active_record"
# require "minitest/autorun"
require "logger"
# メモリ上に DB を構築する
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# ActiveRecord::Base.logger = Logger.new(STDOUT)
# DB の schema を定義する
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.boolean :active
end
end
$stdout = temp_io
class User < ActiveRecord::Base
$debug = true
scope :actives, -> { where(active: true) }
$debug = false
scope(:owners, -> { where(owner: true) }) do
def order_by_created_at(order_ = "asc")
order(created_at: order_)
end
end
end
p User.method :actives
# puts User.owners.order_by_created_at.to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment