ActiveAdmin HMABTM错误消息

时间:2017-07-17 15:40:40

标签: ruby-on-rails activeadmin

如何解决此错误消息?我正在尝试创建一个新对象,我收到以下错误消息。

错误消息: unknown attribute 'w_artisan_id' for ArtisanColour.

如果我从ActiveAdmin表单中删除collection_colour_ids,则不再出现错误,所以我非常肯定它与加入模型有关,我创建了调用artisan_colour

代码如下。

ActiveAdmin.register WArtisan do

    # See permitted parameters documentation:
    # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
    #
    permit_params :name, collection_colour_ids: []
    #


     form do |f|
        f.semantic_errors *f.object.errors.keys
        f.input :name
        f.input :collection_colour_ids, multiple: true, required: true, :label => 'Colours', :as => :check_boxes, :collection => CollectionColour.all.collect {|u| [u.colour_name, u.id]}


        f.actions
     end

    end

型号:

class ArtisanColour < ApplicationRecord
    belongs_to :collection_colour
    belongs_to :w_artisan
    accepts_nested_attributes_for :collection_colour
    accepts_nested_attributes_for :w_artisan
end


class CollectionColour < ApplicationRecord
    has_many :artisan_colours
    has_many :w_artisans, through: :artisan_colours 
    accepts_nested_attributes_for :w_artisans
    accepts_nested_attributes_for :artisan_colours
end


class WArtisan < ApplicationRecord
    has_many :artisan_colours
    has_many :collection_colours, through: :artisan_colours
    accepts_nested_attributes_for :collection_colours
    accepts_nested_attributes_for :artisan_colours

    def self.listing
      order(created_at: :desc)
    end
end

架构:

create_table "artisan_colours", force: :cascade do |t|
    t.integer  "collection_colours_id"
    t.integer  "w_artisans_id"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
    t.index ["collection_colours_id"], name: "index_artisan_colours_on_collection_colours_id"
    t.index ["w_artisans_id"], name: "index_artisan_colours_on_w_artisans_id"
  end

1 个答案:

答案 0 :(得分:1)

数据库迁移不正确。

应该是

t.integer  "w_artisan_id"

t.integer  "w_artisans_id"

默认情况下,外键字段是单数。