简体   繁体   中英

rails admin has_many through relationship leading to errors

I am using rails_admin gem for admin interface.

I have a has_many through relationship which doesn't seem to work with 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

I get error every time I try to create a new company and my guess is that its the first time when rails_admin tries to check the relationships and it doesn't accept my current associations.

The error I get is this file 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] }

I get this error

 undefined method `klass' for nil:NilClass`

Can anyone help me with this association how can I fix it.

perhaps a late response, but could you compare the relevant part of your database schema with the following and let me know the difference? I believe the relations are setup correctly so that should be the problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM