Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fix? ( undefined method name' for nil:NilClass ) #54

Closed
ghost opened this issue May 13, 2013 · 14 comments
Closed

How to fix? ( undefined method name' for nil:NilClass ) #54

ghost opened this issue May 13, 2013 · 14 comments
Labels

Comments

@ghost
Copy link

ghost commented May 13, 2013

rake erd --trace
** Invoke erd (first_time)
** Invoke erd:generate (first_time)
** Invoke erd:options (first_time)
** Execute erd:options
** Invoke erd:load_models (first_time)
** Execute erd:load_models
Loading application environment...
** Invoke environment (first_time)
** Execute environment
Loading code in search of Active Record models...
** Execute erd:generate
Generating Entity-Relationship Diagram for 35 models...
Warning: Ignoring invalid model WiceGridSerializedQuery (table wice_grid_serialized_queries does not exist)
Warning: Ignoring invalid model Old::OldCategory (Unknown database 'snegiri_legacy')
Warning: Ignoring invalid model Old::OldField (Unknown database 'snegiri_legacy')
Warning: Ignoring invalid association :resource on ActiveAdmin::Comment (polymorphic interface Resource does not exist)
Warning: Ignoring invalid association :author on ActiveAdmin::Comment (polymorphic interface Author does not exist)
Warning: Ignoring invalid association :admins on City (uninitialized constant City::Admin)
Warning: Ignoring invalid association :assetable on Ckeditor::Asset (polymorphic interface Assetable does not exist)
Warning: Ignoring invalid association :assetable on Ckeditor::Asset (polymorphic interface Assetable does not exist)
Warning: Ignoring invalid association :assetable on Ckeditor::Asset (polymorphic interface Assetable does not exist)
rake aborted!
undefined method name' for nil:NilClass /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/domain/specialization.rb:66:in<=>'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/domain/specialization.rb:13:in sort' /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/domain/specialization.rb:13:infrom_models'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/domain.rb:67:in specializations' /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/diagram.rb:170:infiltered_specializations'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/diagram.rb:132:in generate' /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/diagram.rb:119:increate'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/diagram.rb:74:in create' /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bundler/gems/rails-erd-d29b0c29fcfa/lib/rails_erd/tasks.rake:41:inblock (2 levels) in <top (required)>'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:246:in call' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:246:inblock in execute'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:241:in each' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:241:inexecute'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:184:in block in invoke_with_call_chain' /home/zheka/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/monitor.rb:211:inmon_synchronize'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:177:in invoke_with_call_chain' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:205:inblock in invoke_prerequisites'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:203:in each' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:203:ininvoke_prerequisites'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:183:in block in invoke_with_call_chain' /home/zheka/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/monitor.rb:211:inmon_synchronize'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:177:in invoke_with_call_chain' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:170:ininvoke'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:143:in invoke_task' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:inblock (2 levels) in top_level'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:in each' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:inblock in top_level'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:110:in run_with_threads' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:95:intop_level'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:73:in block in run' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:160:instandard_exception_handling'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:70:in run' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/bin/rake:33:in<top (required)>'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@global/bin/rake:23:in load' /home/zheka/.rvm/gems/ruby-2.0.0-p0@global/bin/rake:23:in

'
/home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bin/ruby_noexec_wrapper:14:in eval' /home/zheka/.rvm/gems/ruby-2.0.0-p0@snegiri/bin/ruby_noexec_wrapper:14:in'
Tasks: TOP => erd => erd:generate

@rolftimmermans
Copy link
Member

Thanks for your report. Could you include your database schema and ActiveRecord models? If you don't wont to post it here, you can email it to me as well.

@ldonnet
Copy link

ldonnet commented Jun 5, 2013

I have the same issue with my models :

undefined method `name' for nil:NilClass
/home/luc/.rbenv/versions/1.8.7-debian/gems/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:70:in `<=>'
/home/luc/.rbenv/versions/1.8.7-debian/gems/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:13:in `sort'
/home/luc/.rbenv/versions/1.8.7-debian/gems/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:13:in `from_models'

I add some log in my file and I see this :

#<RailsERD::Domain::Entity:0x007f5c0b8096e8 @model=Chouette::ActiveRecord>
#<RailsERD::Domain::Entity:0x007f5c0b8096e8 @model=Chouette::ActiveRecord>
#<RailsERD::Domain::Entity:0x007f5c0b808f68 @model=Chouette::TridentActiveRecord>
nil

for this code :

def <=>(other) # @private :nodoc:
    puts generalized.inspect
    puts other.generalized.inspect
    puts specialized.inspect
    puts other.specialized.inspect
        (generalized.name <=> other.generalized.name).nonzero? or (specialized.name <=> other.specialized.name)
      end

And my class Chouette::TridentActiveRecord is an abstract class.

class Chouette::TridentActiveRecord < Chouette::ActiveRecord
end

module Chouette
  class ActiveRecord < ::ActiveRecord::Base
  end
end

Perhaps we need to test if generalized and specialized present?

@skydan
Copy link

skydan commented Sep 9, 2013

Same issue for me:

Generating Entity-Relationship Diagram for 51 models...
Warning: Ignoring invalid association :tagger on ActsAsTaggableOn::Tagging (polymorphic interface Tagger does not exist)
Warning: Ignoring invalid association :resource on ActiveAdmin::Comment (polymorphic interface Resource does not exist)
Warning: Ignoring invalid association :author on ActiveAdmin::Comment (polymorphic interface Author does not exist)
Warning: Ignoring invalid association :owner on PublicActivity::Activity (polymorphic interface Owner does not exist)
Warning: Ignoring invalid association :recipient on PublicActivity::Activity (polymorphic interface Recipient does not exist)
Warning: Ignoring invalid association :ratings on User (uninitialized constant User::Rating)
Warning: Ignoring invalid association :ratings on User (uninitialized constant User::Rating)
Warning: Ignoring invalid association :ratings on User (uninitialized constant User::Rating)
Warning: Ignoring invalid association :parents_wait_list on Program (Could not find the source association(s) :parents_wait_list or :parents_wait_list in model ProgramParentOrderRelation. Try 'has_many :parents_wait_list, :through => :program_parent_order_relations, :source => <name>'. Is it one of :activities, :program, :line_item, or :parent?)
rake aborted!
undefined method `name' for nil:NilClass
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:111:in `block (2 levels) in specializations_mapping'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:109:in `each'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:109:in `block in specializations_mapping'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:108:in `tap'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:108:in `specializations_mapping'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:81:in `specializations_by_entity_name'
/Users/skydan/.rvm/gems/ruby-1.9.3-p448@global/gems/rails-erd-1.1.0/lib/rails_erd/domain/entity.rb:92:in `children'

@amccloud
Copy link

Same here

undefined method `name' for nil:NilClass
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:66:in `<=>'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:13:in `sort'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/domain/specialization.rb:13:in `from_models'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/domain.rb:67:in `specializations'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:170:in `filtered_specializations'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:132:in `generate'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:119:in `create'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:74:in `create'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/tasks.rake:41:in `block (2 levels) in <top (required)>'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:197:in `block in invoke_prerequisites'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `each'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `invoke_prerequisites'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:174:in `block in invoke_with_call_chain'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/srv/www/example/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/srv/www/example/.rbenv/versions/2.0.0-p247/bin/rake:23:in `load'
/srv/www/example/.rbenv/versions/2.0.0-p247/bin/rake:23:in `<main>'
Tasks: TOP => erd => erd:generate

@rzane
Copy link

rzane commented Mar 17, 2014

I encountered the same issue. It does seem to be related to abstract classes, because that's what went wrong for me. I have two abstract classes in my application.

I changed the <=> method to this, so I could find out where the sort went wrong:

def <=>(other) # @private :nodoc:
  (generalized.name <=> other.generalized.name).nonzero? or (specialized.name <=> other.specialized.name)
rescue
  raise other.inspect
end

Once I found out which class it was, I added this to it, and everything worked fine:

def self.table_exists?
  true
end

It's kind of strange, especially because I only added the table_exists? to one of my two classes.

@codez
Copy link

codez commented Jun 25, 2014

The following patch helped in my case. ERD removed the non-abstract models because it could not connect to their database, but used them again in the Specialization class. The patch makes sure that an entity really exists in the list before creating a specialization for it.

module RailsERD
  class Domain
    class Specialization
      class << self
        def abstract_from_models(domain, models)
          models.select(&:abstract_class?).
                 collect(&:descendants).
                 flatten.
                 select  { |model| domain.entity_by_name(model.name) }.
                 collect { |model|
            new(domain, domain.entity_by_name(model.superclass.name), domain.entity_by_name(model.name))
          }
        end
      end
    end
  end
end

@pcdoc80
Copy link

pcdoc80 commented Jul 4, 2014

@codez: Which file did you put that code in?

@codez
Copy link

codez commented Jul 5, 2014

To fix the issue permanently, this method should be replaced in Rails ERD in lib/rails_erd/domain/specialization.rb.

To fix temporarly in my application, I require it in a rake task that is run before the erd task:

namespace :erd do
  task :options => :customize
  task :customize do
    require 'rails_erd/domain/specialization'
    require Rails.root.join('lib', 'tasks', 'erd_patch.rb')  # this is where above patch resides
  end
end

@bf4
Copy link
Collaborator

bf4 commented Sep 4, 2014

PR?

wuboy0307 added a commit to wuboy0307/rails-erd that referenced this issue Oct 29, 2014
@kerrizor kerrizor added the Bug label Dec 14, 2014
@kerrizor
Copy link
Collaborator

I just pushed a fix for this that uses a NullObject pattern to patch around the error. This should be fixed now; I'll push a new version of the gem (1.2.2) later on this evening.

@ldonnet
Copy link

ldonnet commented Dec 23, 2014

Thanks for this patch

@vaibhav8186
Copy link

@kerrizor @codez This issue still exists in rails-erd (1.6.0). So can you please give your thoughts on that.

@pooja-mane
Copy link

@kerrizor @codez @rolftimmermans I am also facing the same issue as
NoMethodError: undefined method name' for nil:NilClass`
rails-erd(1.6.0)

@karamat
Copy link

karamat commented Oct 9, 2020

I have fixed this issue temporarily and successfully generate ERD diagram. 2 things I have done.

1- Tracing back to the point of crash, and found that it is happening due to relationship.destination nil . So I handled nil check by searching this in all the code

2- After this it crashed again in this method in /lib/rails-erd/domain/relationship.rb

def foreign_key_required?(association)
  if !association.active_record.abstract_class? and association.belongs_to?
    column = association.active_record.columns_hash[association.send(Domain.foreign_key_method_name)] and !column.null
  end
end

I rescued the line that was crashing

def foreign_key_required?(association)
  if !association.active_record.abstract_class? and association.belongs_to?
    begin
      column = association.active_record.columns_hash[association.send(Domain.foreign_key_method_name)] and !column.null
    rescue Exception => e
      p e.message
    end
  end
end

Hope this helps someone

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

No branches or pull requests