Skip to content

Instantly share code, notes, and snippets.

@JuanitoFatas
Forked from senny/issue_6769.rb
Created April 8, 2014 16:15
Show Gist options
  • Save JuanitoFatas/10150063 to your computer and use it in GitHub Desktop.
Save JuanitoFatas/10150063 to your computer and use it in GitHub Desktop.
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