Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created April 14, 2015 10:12
Show Gist options
  • Save yuroyoro/94e55bd05e748b052689 to your computer and use it in GitHub Desktop.
Save yuroyoro/94e55bd05e748b052689 to your computer and use it in GitHub Desktop.
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
create_table :events do |t|
t.belongs_to :item
end
create_table :event_people do |t|
t.belongs_to :event
t.belongs_to :person
end
create_table :people do |t|
end
end
end
class Item < ActiveRecord::Base
has_many :events
has_many :event_people, :through => :events
has_many :people, :through => :events
end
class Event < ActiveRecord::Base
belongs_to :item
has_many :event_people
has_many :people, :through => :event_people
end
class EventPerson < ActiveRecord::Base
belongs_to :event
belongs_to :person
end
class Person < ActiveRecord::Base
end
CreateModels.new.change
puts "Populating Database"
1000.times do
Item.create
end
Item.all.each_with_index do |item|
event = Event.new
event.people.build
event.people.build
item.events << event
end
rescue => e
# Tables already exist
# puts e.message
end
# ::RubyProf.start
StackProf.run(mode: :cpu, out: "./stackprof-cpu-prelading-4.2-#{Time.now.strftime('%F-%H-%M-%S')}.dump") do
Item.preload(:people).limit(1000).order(:id).to_a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment