Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Last active August 29, 2015 13:56
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 nelsnelson/9284736 to your computer and use it in GitHub Desktop.
Save nelsnelson/9284736 to your computer and use it in GitHub Desktop.
Occasionally, when I run this code in a multi-threaded environment, I observe the provided error when accessing for example apple.tags.
$ ./tags.rb
apple
edible
undefined method `push' for nil:NilClass
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:1231:in `def_many_to_many'
org/jruby/RubyArray.java:1617:in `each'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:1231:in `def_many_to_many'
org/jruby/RubyArray.java:1617:in `each'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/dataset/actions.rb:48:in `all'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/plugins/eager_each.rb:41:in `all'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:1223:in `def_many_to_many'
org/jruby/RubyProc.java:255:in `call'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:2187:in `eager_load'
org/jruby/RubyArray.java:1617:in `each'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:2151:in `eager_load'
org/jruby/RubyBasicObject.java:1715:in `__send__'
org/jruby/RubyKernel.java:2217:in `send'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/plugins/tactical_eager_loading.rb:47:in `load_associated_objects'
/usr/java/jruby-1.7.4/lib/ruby/gems/shared/gems/sequel-4.7.0/lib/sequel/model/associations.rb:1179:in `tags'
/tmp/tags.rb:80:in `tags'
#! /usr/bin/env jruby
require 'sequel'
`psql -U postgres -c 'DROP DATABASE test'`
`psql -U postgres -c 'CREATE DATABASE test'`
`psql -U postgres -c "DROP USER test"`
`psql -U postgres -c "CREATE USER test PASSWORD 'test'"`
Sequel.connect 'jdbc:postgresql://localhost/test?user=test&password=test'
Sequel::Model.plugin :tactical_eager_loading
Sequel::Model.plugin :eager_each
Sequel::Model.db.create_table? :objects do
primary_key :id
index :name
String :name
String :object_type, :null => false
end
Sequel::Model.db.create_table! :tags do
primary_key :id
index :name
String :name, :unique => true, :null => false
end
Sequel::Model.db.create_table! :tagged do
primary_key :id
foreign_key :object_id, :objects, :on_delete => :cascade
foreign_key :tag_id, :tags, :on_delete => :cascade
end
module Named
def to_s; self.name; end
end
class Tag < Sequel::Model
include Named
end
class Tagged < Sequel::Model
end
module Taggable
def tag(*args)
a = args.flatten.collect!(&:to_s)
for tag in a - (tags & a)
add_tag Tag.find_or_create(:name => tag)
end
save
end
def untag(*args)
for tag in tags & args.flatten.collect!(&:to_s)
remove_tag Tag.find(:name => tag)
end
save
end
end
module Z
class Object < Sequel::Model
include Named
include Taggable
plugin :single_table_inheritance, :object_type
plugin :tactical_eager_loading
one_to_many :tagged, :key => :object_id
many_to_many :tags, :class => 'Tag', :join_table => :tagged, :left_key => :object_id, :right_key => :tag_id
end
end
apple = Z::Object.create(:name => 'apple')
apple.tag :edible
puts apple
puts apple.tags
Sequel::Model.db.disconnect
`psql -U postgres -c 'DROP DATABASE test'`
`psql -U postgres -c "DROP USER test"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment