2013-04-08 38 views
6

Tôi mới dùng đường ray và tôi đã cố gắng để có hai has_many: mặc dù các mối quan hệ để làm việc (trái ngược với việc sử dụng has_and_belongs_to_many như được giải thích qua đường bưu điện này http://blog.flatironschool.com/post/35346328762/why-you-dont-need-has-and-belongs-to-many) nhưng bây giờ chạy vào một lỗi Postgres:Rails has_many: thông qua PG :: Lỗi: LRI: thông qua tham chiếu cột "id" là lỗi mơ hồ

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 :title 
    = f.text_field :title 
    .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, 
          :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 

component.rb

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 

collection_components bảng

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) 

bộ sưu tập bảng

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) 

thành phần bảng

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) 
+0

Ngoài ra, bảng danh mục đã được điền trước khi thiết lập mối quan hệ has_many_though và bảng bộ sưu tập trống. –

+0

Tôi không chắc liệu điều này có tạo nên sự khác biệt hay không nhưng khi tôi tạo ra mô hình collection_component, tôi đã tạo nó thành "collection_components" và sau đó gỡ bỏ thủ công "s" –

+0

Phương tiện xóa thủ công? Bạn đã xóa từ đâu? – codeit

Trả lời

14

Hãy thử điều này:

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

Khi bạn thực hiện một tăng dần join trên id trở thànhCộtvì cả hai cột componentscollections đều có cột id. Nó sẽ không biết sử dụng.

+0

Vâng, đã làm điều đó, cảm ơn! –

+0

Tôi gặp vấn đề tương tự khi tôi thực hiện một phạm vi để đặt hàng các bài đăng trên blog mà không cần chỉ định bảng và PG bị nóng và làm phiền khi tôi cố gắng kết xuất một bộ sưu tập các partials. Điều này đã làm các trick, cảm ơn. –

+0

Cảm ơn rất nhiều! Giải thích ngắn gọn và súc tích! – hernanvicente

Các vấn đề liên quan