1

我有一個傳統的Rails 3應用程序,我試圖在兩個模型之間建立一個簡單的HABTM關係:VolunteerCircle和User。我在另外兩個模型中也有同樣的關聯。Rails 3 HABTM產生ActiveRecord :: Associations :: CollectionProxy對象

的關聯如下:

VolunteerCircle型號:

class VolunteerCircle < ActiveRecord::Base 
    #... 

    has_and_belongs_to_many :users 

    # ... 
end 

用戶模式:

class User < ActiveRecord::Base 
    #... 

    has_and_belongs_to_many :volunteer_circles 

    #... 
end 

這裏是遷移:

class CreateVolunteerCirclesUsers < ActiveRecord::Migration 
    def change 
    create_table :volunteer_circles_users, id: false do |t| 
     t.belongs_to :volunteer_circle, index: true 
     t.belongs_to :user, index: true 
    end 
    end 
end 

當我打電話#用戶一個VolunteerCircle對象(反之亦然),Rails咳嗽一個ActiveRecord :: Associations :: CollectionProxy對象。

volunteer_circles_users表在數據庫中對我來說很合適。我在這裏錯過了什麼?

此外,我知道HABTMT的優越性。超出我的控制範圍在這裏指定HABTM。

非常感謝!

回答

0

有一個很難試圖在這裏找到錯誤。但也許其中的一些將幫助您:

我認爲你缺少的多場指數:

add_index : volunteer_circles_users, [:volunteer_circles_id, :user_id] 
add_index : volunteer_circles_users, [:user_id, :volunteer_circles_id] 

而且create_join_table創建它按字母順序排列,因此它將代替user_volunteer_circles。所以,也許你可能需要做一些事情,如:

class VolunteerCircle < ActiveRecord::Base 
    has_and_belongs_to_many :users, :join_table => : volunteer_circles_users 
end 

希望它可以幫助..

+0

了有關按字母順序排列的說明釘它。感謝您的信息! – cmhobbs

相關問題