簡體   English   中英

nil:NilClass的未定義方法“ klass”

[英]undefined method `klass' for nil:NilClass

我是Rails的新手,我對ORM關系有疑問。

當我訪問/ admin / health_concerns_lists

顯示出來。

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)

這是我的關系。

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

架構圖

health_profile
id
name

health_concerns
id
health_profile_id
health_concerns_id

health_concerns_list
id
name

我知道,我的命名約定聽起來不對。

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
    has_many :health_concerns_list
end

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

嘗試改變

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

變成:

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

對於具有has_many through: ...每個模型, has_many through: ...

嘗試刪除:

self.table_name = "health_concerns_list"

根據Rails約定,您無需為模型指定表名。 它們的型號名稱( HealthConcernsList )將tableize倒是給health_conerns_lists

如果您必須使用單數table_name,請嘗試:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM