olleh
olleh

Reputation: 1277

undefined method `klass' for nil:NilClass

I'm a newbie in Rails and I have a problem with ORM relationships.

When i access /admin/health_concerns_lists

This shows up.

Showing /Users/sbpipb/.rvm/gems/ruby-2.1.1/bundler/gems/activeadmin-a2cd9604c2d9/app/views/active_admin/resource/index.html.arb where line #2 raised:

undefined method `klass' for nil:NilClass
Extracted source (around line #2):
1

  insert_tag renderer_for(:index)

Here is my relationships.

class HealthProfile < ActiveRecord::Base
    #table - health_profiles
    has_many :health_concerns, dependent: :destroy
    has_many :health_concerns_lists, :through => :health_concerns ,dependent: :destroy
end

class HealthConcern < ActiveRecord::Base
    #table - health_concerns
    belongs_to :health_profile
    belongs_to :health_concerns_list
end

class HealthConcernsList < ActiveRecord::Base
    self.table_name = "health_concerns_list"
    has_many :health_concerns, dependent: :destroy
    has_many :health_profiles, :through => :health_concerns_lists, dependent: :destroy
end

Schema

health_profile
id
name

health_concerns
id
health_profile_id
health_concerns_id

health_concerns_list
id
name

I know, my naming convention sounds wrong.

Upvotes: 2

Views: 10151

Answers (3)

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41865

Try changing

class HealthProfile < ActiveRecord::Base
    # ...
    has_many :health_concerns_lists, :through => :health_concerns, dependent: :destroy
end

into:

class HealthProfile < ActiveRecord::Base
    # ...
    has_many :health_concerns_lists, :through => :health_concerns, dependent: :destroy
    has_many :health_concerns
end

For every model with has_many through: ...

Upvotes: 3

sealocal
sealocal

Reputation: 12407

Try removing:

self.table_name = "health_concerns_list"

By Rails conventions you do not need to specify a table name for the model. Them model name (HealthConcernsList) will be tableize'd to health_conerns_lists.

If you must us a singular table_name, try:

class HealthConcernsList < ActiveRecord::Base
  self.pluralize_table_names = false
end

http://guides.rubyonrails.org/3_1_release_notes.html#active-record

Upvotes: 0

Shatabdi Mohapatra
Shatabdi Mohapatra

Reputation: 467

class HealthProfile < ActiveRecord::Base
    #table - health_profiles
    has_many :health_concerns, dependent: :destroy
    has_many :health_concerns_lists, :through => :health_concerns ,dependent: :destroy
end

I guess you need to correct this part:

class HealthConcern < ActiveRecord::Base
    #table - health_concerns
    belongs_to :health_profile
    has_many :health_concerns_list
end

class HealthConcernsList < ActiveRecord::Base
    belongs_to :health_concerns, dependent: :destroy
    belongs_to :health_profiles, dependent: :destroy
end

Upvotes: 3

Related Questions