【问题标题】:rails admin has_many through relationship leading to errorsrails admin has_many through 关系导致错误
【发布时间】:2015-12-01 19:03:23
【问题描述】:

我正在使用 rails_admin gem 作为管理界面。

我有一个 has_many through 关系,这似乎不适用于 rails admin。

class Company < ActiveRecord::Base
    has_many :talent_infos, class_name: 'CompanyTalentInfo'
    has_many :talents, through: :talent_infos
end

class CompanyTalentInfo < ActiveRecord::Base
    belongs_to :company
    belongs_to :talent
end

class Talent < ActiveRecord::Base
    has_many :talent_infos, class_name: 'CompanyTalentInfo'
    has_many :companies, through: :talent_infos
end

每次我尝试创建新公司时都会出错,我猜这是 rails_admin 第一次尝试检查关系并且它不接受我当前的关联。

我得到的错误是这个文件 gems/rails_admin-0.7.0/app/views/rails_admin/main/_form_filtering_multiselect.html.haml:21

21     controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated    _object_label_method), o.send(field.associated_primary_key)] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.si    ze, i+=1] }

我收到此错误

 undefined method `klass' for nil:NilClass`

谁能帮我解决这个关联,我该如何解决它。

【问题讨论】:

    标签: ruby-on-rails ruby rails-admin


    【解决方案1】:

    可能响应较晚,但您能否将您的数据库架构的相关部分与以下内容进行比较,让我知道其中的区别?我相信关系设置正确,所以这应该是问题所在。

    create_table "companies", force: :cascade do |t|
      t.string "name"
    end
    
    create_table "company_talent_infos", force: :cascade do |t|
      t.string  "metadata"
      t.integer "company_id"
      t.integer "talent_id"
    end
    
    create_table "talents", force: :cascade do |t|
      t.string "name"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      相关资源
      最近更新 更多