-
-
Save dvandersluis/12c3f262791a0475096667fc9d535e4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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