Skip to content

Instantly share code, notes, and snippets.

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 kevinhq/85f31d49fb3c931afac14f0227fe3dc4 to your computer and use it in GitHub Desktop.
Save kevinhq/85f31d49fb3c931afac14f0227fe3dc4 to your computer and use it in GitHub Desktop.
Confirm Rails 4 behavior for includes inside has_many scope
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.2.10'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
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 :concepts do |t|
t.string :name, default: 'Concept test'
end
create_table :links do |t|
t.references :concept
t.references :entry
t.datetime :starts_at
end
create_table :entries do |t|
t.string :name, default: '1st entry'
end
create_table :scores do |t|
t.integer :score, default: 0
t.references :entry
end
end
class Score < ActiveRecord::Base
belongs_to :concept
end
class Concept < ActiveRecord::Base
has_many :links
has_many :entries, through: :links
has_many :top_entries,->{ includes(:score).order('scores.score DESC').references(:score).limit(3) }, through: :links, source: :entry
end
class Link < ActiveRecord::Base
belongs_to :concept
belongs_to :entry
end
class Entry < ActiveRecord::Base
has_one :score
has_many :links
end
class BugTest < Minitest::Test
def test_association_stuff
concept = Concept.create!
concept2 = Concept.create!(name: '2nd concept test')
entry = Entry.create!
entry2 = Entry.create!(name: '2nd entry test')
entry3 = Entry.create!(name: '3rd entry test')
entry4 = Entry.create!(name: '4th entry test')
entry5 = Entry.create!(name: '5th entry test')
entry6 = Entry.create!(name: '6th entry test')
entry7 = Entry.create!(name: '7th entry test')
concept.links << Link.create!(starts_at: DateTime.current, entry_id: entry.id)
concept.links << Link.create!(starts_at: DateTime.current, entry_id: entry6.id)
concept.links << Link.create!(starts_at: DateTime.current, entry_id: entry7.id)
concept2.links << Link.create!(starts_at: DateTime.current, entry_id: entry2.id)
concept2.links << Link.create!(starts_at: DateTime.current, entry_id: entry3.id)
concept2.links << Link.create!(starts_at: DateTime.current, entry_id: entry4.id)
concept2.links << Link.create!(starts_at: DateTime.current, entry_id: entry5.id)
entry.score = Score.create!(score: 0)
entry2.score = Score.create!(score: 1)
entry3.score = Score.create!(score: 2)
entry4.score = Score.create!(score: 3)
entry5.score = Score.create!(score: 4)
entry6.score = Score.create!(score: 5)
entry7.score = Score.create!(score: 6)
concepts = Concept.includes(:top_entries)
concepts.each_with_index do |c,i|
puts "----- Concept number #{i} ------------"
puts c.top_entries.map(&:name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment