[英]Inserting a value has_and_belongs_to_many associated table on Rails
[英]Rails searches the mistaken has_and_belongs_to_many table
我想在我的文檔表單的選擇框中顯示與特定組織相關的所有類型。 類型是Ar引擎的一部分。 組織是另一個現有引擎的一部分。
module Ar
module OrganisationPatch
extend ActiveSupport::Concern
included do
attr_accessible :ar_document_id
has_many :ar_documents, :class_name => 'Ar::Document'
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type'
end
end
end
module Ar
class Type < ActiveRecord::Base
attr_accessible :name
has_many :documents
has_and_belongs_to_many :organisations
end
end
class CreateTypeOrganisations < ActiveRecord::Migration
def change
create_table :ar_type_organisations, id: false do |t|
t.uuid :type_id, index: true
t.uuid :organisation_id, index: true
end
end
end
在我的documents_controller中,我加載有關before過濾器的表單的類型。 上級返回組織對象:
def load_form_objects
unless current_user.admin?
@types = current_user.superior.ar_types
else
@types = Type.all
end
end
呼叫該頁面時,我收到此錯誤,並問我為什么他要尋找一個名為organisations_types的表:
ActiveRecord :: StatementIn Ar / documents#new中無效
Mysql2 ::錯誤:表'portal.organisations_types'不存在:SELECT
ar_types
。* FROMar_types
INNER JOINorganisations_types
ONar_types
。id
=organisations_types
。type_id
WHEREorganisations_types
。organisation_id
= x'891c3986b33845d08d3951645a4f27d5'
有人知道我在這里做錯了嗎?
您的表名has_and_belongs_to_many
期望的詞匯順序進行映射。 (預期順序為organisations_types
)
因此,您必須在兩個模型的關聯中添加:join_table
選項。 像這樣,
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type', join_table: "ar_type_organisations"
has_and_belongs_to_many :organisations, join_table: "ar_type_organisations"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.