Skip to content

Instantly share code, notes, and snippets.

@dvandersluis
Last active July 20, 2020 17:03
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 dvandersluis/12c3f262791a0475096667fc9d535e4b to your computer and use it in GitHub Desktop.
Save dvandersluis/12c3f262791a0475096667fc9d535e4b to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "6.0.3.2"
gem "sqlite3"
end
require "active_record"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :authors, force: true do |t|
end
create_table :posts, force: true do |t|
t.references :author
t.timestamps
end
end
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
validate :rate_limit
def rate_limit
if Post.where(author: author_id).where('created_at > ?', 1.hour.ago).any?
errors[:base] << 'Rate limit reached'
end
end
end
Post.where(author_id: 1).first_or_create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment