Skip to content

Instantly share code, notes, and snippets.

@LeeXGreen
Created June 25, 2011 15:41
Show Gist options
  • Save LeeXGreen/1046599 to your computer and use it in GitHub Desktop.
Save LeeXGreen/1046599 to your computer and use it in GitHub Desktop.
Bug with DataMapper 1.1.0, Ruby 1.8.6 -- undefined method 'first'
# Note -- DataMapper no longer officially supports 1.8.6.
# That's why this problem happens.
# For best results, switch to Ruby >= 1.8.7.
# However, if you simply cannot upgrade, this might help.
class Banner
include DataMapper::Resource
property :id, Serial
property :message_id, Integer, :required => true
belongs_to :message
end
class Message
include DataMapper::Resource
property :id, Serial
has n, :banners
end
Banner.first.message_id # works
Message.get(Banner.first.message_id) # works
Banner.first.message # generates exception below
exception_string = <<'EOS'
NoMethodError: undefined method `first' for [#<DataMapper::Property::Serial @model=Bliss::Message @name=:id>]:DataMapper::PropertySet
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/query.rb:67:in `target_conditions'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/associations/many_to_one.rb:79:in `source_scope'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/associations/relationship.rb:156:in `query_for'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/repository.rb:114:in `scope'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/associations/relationship.rb:153:in `query_for'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/associations/many_to_one.rb:97:in `resource_for'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/associations/many_to_one.rb:181:in `lazy_load'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource/state/persisted.rb:23:in `lazy_load'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource/state/persisted.rb:8:in `get'
from /usr/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/model/relationship.rb:345:in `message'
from (irb):1
EOS
module Enumerable
unless instance_methods.include?("first")
def first(num = nil)
if num == nil
holder = nil
catch(:got_element) do
each do |e|
holder = e
throw(:got_element)
end
end
holder
else
holder = []
catch(:got_elements) do
each do |e|
holder << e
throw(:got_element) if num == 0
num -= 1
end
end
holder
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment