Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

undefined method `include?' for nil:NilClass in 1.2.0 but not 1.1.0 #159

Closed
chriscummings opened this issue Oct 27, 2011 · 8 comments
Closed

Comments

@chriscummings
Copy link

I'm using DataMapper with Sinatra and I've found that I keep getting the following error with DataMapper 1.2.0:

NoMethodError at /tag 
undefined method `include?' for nil:NilClass

    file: resource.rb
    location: block in attributes=
    line: 332

Backtrace

/Users/chris/Desktop/chriscummings-Blog-9331485/controllers/tag.rb in new

    tag = Tag.new({

/Users/chris/Desktop/chriscummings-Blog-9331485/controllers/tag.rb in block in <top (required)>

    tag = Tag.new({

/Users/chris/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb in service

    si.service(req, res)

/Users/chris/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb in run

    server.service(req, res)

/Users/chris/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb in block in start_thread

    block ? block.call(sock) : run(sock)

There's nothing wrong with the file mentioned controllers/tag.rb. I get the same error with any path that tries to create/update/delete a record. If I force gem 'data_mapper', '= 1.1.0' in my Gemfile I don't experience the issue.

Potentially pertinent code


controllers/tag.rb
Like I said, the error pops up on any path that attempts to edit a record. Additionally, the require_administrative_privileges and tag_validate! methods don't do anything magical and commenting them out makes no difference.

post '/tag/?' do
  require_administrative_privileges
  tag_validate!(params)

  #Ensure uniques
  tag = Tag.first(:name => params[:name])
  if tag
    halt 409, "Tag \"" + tag.name + "\" already exists!"
  end

  tag = Tag.new({
    :name => params[:name],
    :created_at => Time.now
  })

  if tag.save
    status(201)
    redirect '/tag'
  else
    halt 409, tag.errors.inspect
  end
end

My DataMapper models file

class Post
  include DataMapper::Resource

  property :id,         Serial
  property :created_at, DateTime
  property :updated_at, DateTime
  property :title,      Text
  property :body,       Text
  property :body_md,   Text
  property :slug,       Text

  has n, :posttags
  has n, :tags, :through => :posttags
end

class Tag
  include DataMapper::Resource

  property :id,         Serial
  property :created_at, DateTime
  property :updated_at, DateTime
  property :name,       Text

  has n, :posttags
  has n, :posts, :through => :posttags
end


class Posttag
  include DataMapper::Resource

  property :id,         Serial

  belongs_to :tag, :key => true
  belongs_to :post, :key => true
end

class Page
  include DataMapper::Resource

  property :id,         Serial
  property :created_at, DateTime
  property :updated_at, DateTime
  property :title,      Text
  property :body,       Text
  property :body_md,   Text
  property :slug,       Text
end
@solnic
Copy link
Contributor

solnic commented Oct 27, 2011

You forgot to call DataMapper.finalize

@chriscummings
Copy link
Author

Right you are. Thanks, solnic!

Looking at the datamapper.org page again, the necessity or even the existence of this method isn't super obvious. It might be beneficial to document it a little more loudly for TL;DR folks like myself or maybe raise an exception?

As far as I'm concerned this issue can be can be closed. I don't know what the GitHub etiquette is on closing issues on other's repos.

@emmanuel
Copy link
Member

Hi Chris,

On Oct 26, 2011, at 10:42 PM, Chris Cummings wrote:

I'm using DataMapper with Sinatra and I've found that I keep getting the following error with DataMapper 1.2.0:

Call DataMapper.finalize to get everything ready for use.

Behavior changed a bit in 1.2.0—calling DataMapper.finalize used to be strongly recommended, but is now required.

NoMethodError at /tag 
undefined method `include?' for nil:NilClass

   file: resource.rb
   location: block in attributes=
   line: 332

The failure behavior could be improved...

Hope that helps,
Emmanuel

@chriscummings
Copy link
Author

Hope that helps

Indeed it does. Thanks, Emmanuel.

@youens
Copy link

youens commented Nov 15, 2011

Ran into this as well... a more obvious failure behavior would be great!

@solnic
Copy link
Contributor

solnic commented Nov 15, 2011

@youens failure behavior will be improved in 1.3.0

@gtd
Copy link

gtd commented Jan 13, 2012

I appreciate the issue being here as I found it quickly when I ran into the same thing.

@shangyiss
Copy link

Finalize Models
After declaring all of the models, you should finalize them:

DataMapper.finalize

This issue is troubled me more times,thanks .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants