Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created January 2, 2013 11:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pixeltrix/4433870 to your computer and use it in GitHub Desktop.
Save pixeltrix/4433870 to your computer and use it in GitHub Desktop.
Simple script to reproduce segfaults with Active Record in ruby-1.9.3p362. This style of scope is used by the kaminari gem to define its page scope - using a class method to define default_scope doesn't segfault.
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|
t.string :title
t.timestamps
end
end
class Post < ActiveRecord::Base
default_scope { where(:published => true) }
scope :page, Proc.new { |num| limit(10).offset(10 * (num - 1)) } do
def current_page
return 1 if limit_value.nil?
(offset_value / limit_value) + 1
end
end
end
30.times do |i|
Post.create! do |p|
p.title = "Post #{i}"
end
end
# Segfaults
Post.page(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment