Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails has_many:通过PG ::错误:错误:列引用“id”是模糊错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是rails的新手,我一直在尝试获得两个has_many:然关系可以解决(而不是像本文 http://blog.flatironschool.com/post/35346328762/why-you-dont-need-has-and-belongs-to-many所解释的那样使用has_and_belongs_to_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...
                                                             ^

_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 @R_821_6964@
    = f.text_field @R_821_6964@
  .actions
    = f.submit 'Save'

collection_component.rb

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

  belongs_to      :collection
  belongs_to      :component
end

collection.rb

class Collection < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :style_id,:name,@R_821_6964@,: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   @R_821_6964@

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

component.rb

class Component < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :category_id,: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   @R_821_6964@

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

collection_components表

column     |            Type             |                             Modifiers                              
---------------+-----------------------------+--------------------------------------------------------------------
 id            | Integer                     | not null default nextval('collection_components_id_seq'::regclass)
 collection_id | Integer                     | 
 component_id  | Integer                     | 
 created_at    | @R_673_6561@amp without time zone | not null
 updated_at    | @R_673_6561@amp 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 | @R_673_6561@amp without time zone | not null
 updated_at | @R_673_6561@amp 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  | @R_673_6561@amp without time zone | not null
 updated_at  | @R_673_6561@amp 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上升的连接变为不明确的列时,因为组件和集合都具有id列.它不知道使用哪个.

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – Rails has_many:通过PG ::错误:错误:列引用“id”是模糊错误全部内容,希望文章能够帮你解决ruby-on-rails – Rails has_many:通过PG ::错误:错误:列引用“id”是模糊错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。