Ruby on rails Rails有“许多:通过PG::Error:Error:column reference”;id";这是一个模棱两可的错误

Ruby on rails Rails有“许多:通过PG::Error:Error:column reference”;id";这是一个模棱两可的错误,ruby-on-rails,postgresql,has-many-through,Ruby On Rails,Postgresql,Has Many Through,我是rails新手,我一直在尝试两个has\u-many:虽然需要解决关系(与使用has\u和\u-allown\u-many相反,正如本文所解释的),但现在遇到了一个Postgres错误: PG::Error: ERROR: column reference "id" is ambiguous LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM...

我是rails新手,我一直在尝试两个has\u-many:虽然需要解决关系(与使用has\u和\u-allown\u-many相反,正如本文所解释的),但现在遇到了一个Postgres错误:

PG::Error: ERROR:  column reference "id" is ambiguous
LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM...
                                                             ^
: SELECT  1 AS one FROM "components" INNER JOIN "collection_components" ON "components"."id" = "collection_components"."component_id" WHERE "collection_components"."collection_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIMIT 1
  Rendered collections/_form.html.haml (117.0ms)
  Rendered collections/new.html.haml within layouts/application (143.5ms)
Completed 500 Internal Server Error in 164ms

ActiveRecord::StatementInvalid - PG::Error: ERROR:  column reference "id" is ambiguous
LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM...
                                                             ^
\u form.html.haml

= form_for @collection do |f|
  - if @collection.errors.any?
    #error_explanation
      %h1= "#{pluralize(@collection.errors.count, "error")} prohibited this collection from being saved:"
      %ul
        - @collection.errors.full_messages.each do |msg|
          %li= msg

  .field
    - Component.all.each do |component|
      = label_tag :component_ids, component.id
      = check_box_tag :component_ids, component.id, @collection.components.include?(component), :name => 'collection[component_ids][]'
  .field
    = f.label :title
    = f.text_field :title
  .actions
    = f.submit 'Save'
集合\u组件.rb

class CollectionComponent < ActiveRecord::Base
  attr_accessible :collection_id, 
                  :component_id

  belongs_to      :collection
  belongs_to      :component
end
class Collection < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :style_id, 
                          :name, 
                          :title,
                          :component

  #has_and_belongs_to_many :components

  has_many                :collection_components, :dependent => :destroy
  has_many                :components, :through => :collection_components

  belongs_to              :style

  validates_presence_of   :style
  validates_presence_of   :title

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end
class Component < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :category_id, 
                          :name, 
                          :title,
                          :collection,
                          :style

  has_many                :collection_components, :dependent => :destroy
  has_many                :collections,   :through => :collection_components

  has_many                :component_styles
  has_many                :styles,        :through => :component_styles

  belongs_to              :category

  validates_presence_of   :category
  validates_presence_of   :title

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end
集合表

Column     |            Type             |                             Modifiers                              
---------------+-----------------------------+--------------------------------------------------------------------
 id            | integer                     | not null default nextval('collection_components_id_seq'::regclass)
 collection_id | integer                     | 
 component_id  | integer                     | 
 created_at    | timestamp without time zone | not null
 updated_at    | timestamp without time zone | not null
Indexes:
    "collection_components_pkey" PRIMARY KEY, btree (id)  
Column   |            Type             |                        Modifiers                         
------------+-----------------------------+----------------------------------------------------------
 id         | integer                     | not null default nextval('collections_id_seq'::regclass)
 style_id   | integer                     | 
 name       | character varying(255)      | 
 title      | character varying(255)      | 
 created_at | timestamp without time zone | not null
 updated_at | timestamp without time zone | not null
Indexes:
    "collections_pkey" PRIMARY KEY, btree (id)
   Column    |            Type             |                        Modifiers                        
-------------+-----------------------------+---------------------------------------------------------
 id          | integer                     | not null default nextval('components_id_seq'::regclass)
 name        | character varying(255)      | 
 title       | character varying(255)      | 
 category_id | integer                     | 
 created_at  | timestamp without time zone | not null
 updated_at  | timestamp without time zone | not null
Indexes:
    "components_pkey" PRIMARY KEY, btree (id)
组件表

Column     |            Type             |                             Modifiers                              
---------------+-----------------------------+--------------------------------------------------------------------
 id            | integer                     | not null default nextval('collection_components_id_seq'::regclass)
 collection_id | integer                     | 
 component_id  | integer                     | 
 created_at    | timestamp without time zone | not null
 updated_at    | timestamp without time zone | not null
Indexes:
    "collection_components_pkey" PRIMARY KEY, btree (id)  
Column   |            Type             |                        Modifiers                         
------------+-----------------------------+----------------------------------------------------------
 id         | integer                     | not null default nextval('collections_id_seq'::regclass)
 style_id   | integer                     | 
 name       | character varying(255)      | 
 title      | character varying(255)      | 
 created_at | timestamp without time zone | not null
 updated_at | timestamp without time zone | not null
Indexes:
    "collections_pkey" PRIMARY KEY, btree (id)
   Column    |            Type             |                        Modifiers                        
-------------+-----------------------------+---------------------------------------------------------
 id          | integer                     | not null default nextval('components_id_seq'::regclass)
 name        | character varying(255)      | 
 title       | character varying(255)      | 
 category_id | integer                     | 
 created_at  | timestamp without time zone | not null
 updated_at  | timestamp without time zone | not null
Indexes:
    "components_pkey" PRIMARY KEY, btree (id)
试试这个:

  default_scope { order('collections.id ASC') } //collection.rb
  default_scope { order('components.id ASC') } //component.rb

当您在
id
列上执行
join
升序时,由于
组件
集合
都有
id
列,因此
列变得不明确。它将不知道使用哪一个。

您只需要在查询中添加字段前的表名

sql方法(“name-of-table.field运算符”)

示例:
订单(“collections.id ASC”)


发生这种情况的原因是存在许多同名的字段,而解释器无法识别查询集合中的成员。

此外,类别表在设置has_many_Through关系之前已填充,集合表为空。我不确定这是否有区别,但当我生成集合组件模型时,我将其生成为“集合组件”,然后手动删除“s”,手动删除意味着??从何处删除?如中所示,我打开了rb文件,将类名从“CollectionComponents”更改为“CollectionComponent”,并将文件从“collection\u components.rb”重命名为“collection\u component.rb”,打开rails控制台并运行
CollectionComponent
。它工作正常吗?我遇到了一个类似的问题,我在没有指定表的情况下创建了一个范围来订购博客文章,当我试图呈现一组片段时,PG变得很热,很麻烦。这就成功了,谢谢。非常感谢!简明扼要的解释!