-
-
Save JuanitoFatas/10150063 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
require 'active_record' | |
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :users do |t| | |
t.timestamps | |
end | |
create_table :activities do |t| | |
t.integer :user_id | |
t.timestamps | |
end | |
end | |
class User < ActiveRecord::Base | |
has_many :activities, :order => "created_at desc" | |
end | |
class Activity < ActiveRecord::Base | |
end | |
u = User.create | |
a2 = u.activities.create | |
a1 = u.activities.create(created_at: 1.day.ago) | |
a3 = u.activities.create | |
users = User.includes(:activities).where("activities.created_at < ?", Time.now).to_a | |
expected_ids = [a3.id, a2.id, a1.id] | |
actual_ids = users.first.activities.map(&:id) | |
if expected_ids != actual_ids | |
raise "Expected #{expected_ids} to match #{actual_ids}" | |
else | |
puts "PASSED!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment