已经和属于许多协会并不起作用

时间:2018-01-02 07:36:01

标签: ruby-on-rails activerecord

我对这个HABTM协会感到疯狂。

我有2个班级

(c2)a.x

品牌类表位于另一个数据库中。

我有我的迁移

class Brand < ApplicationRecord
  establish_connection Rails.application.config.dday_companies_database_configuration
  scope :default_order, -> { order :name }
  has_and_belongs_to_many :contents, join_table: :brands_contents, foreign_key: :content_id
end

class Content < ApplicationRecord
 has_and_belongs_to_many :brands, class_name: 'Brand', join_table: :brands_contents, association_foreign_key: :brand_id
end

所以我有我的联接表

    class CreateContentBrandJoinTable < ActiveRecord::Migration[5.1]
  def change
    create_join_table :brands, :contents do |t|
      t.index [:brand_id, :content_id]
    end
  end
end

当我尝试我的关联时,我有这个错误

  

[6] pry(main)&gt; c.brands.first ActiveRecord :: StatementInvalid:   PG :: UndefinedTable:错误:关系&#34; content_brands&#34;不存在   第8行:a.attrelid =   &#39;&#34; content_brands&#34;&#39; :: regcla ...                                             ^:选择a.attname,format_type(a.atttypid,a.atttypmod),                        pg_get_expr(d.adbin,d.adrelid),a.attnotnull,a.atttypid,a.atttypmod,                        c.collname,col_description(a.attrelid,a.attnum)AS评论                   来自pg_attribute a                   LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum                   LEFT JOIN pg_type t ON a.atttypid = t.oid                   LEFT JOIN pg_collat​​ion c ON a.attcollat​​ion = c.oid AND a.attcollat​​ion&lt;&gt; t.typcollat​​ion                  在哪里a.attrelid =&#39;&#34; content_brands&#34;&#39; :: regclass                    和a.attnum&gt; 0而不是a.attisdropped                  订购a.attnum

为什么要寻找-- -- Name: brands_contents; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE brands_contents ( brand_id integer NOT NULL, content_id bigint NOT NULL ); ??

0 个答案:

没有答案