2016-04-02 64 views
0

我有一個結構化的這樣一個數據庫Rails應用程序:database structureRails的映射數據

用戶選擇一些專業方案,這一切都保存爲預期的,同爲配置文件,配置文件選擇一些專業方案和預期都被保存。我可以找到與專業知識相關的簡介。

用戶模型:

has_and_belongs_to_many :expertises 

專業模型:

has_and_belongs_to_many :profiles 
has_and_belongs_to_many :users 

現在,讓我們假設一個用戶選擇一些專長,我想顯示所有具有相同專長的配置?任何想法如何做到這一點?

這可以在控制器中完成,還是需要創建另一個連接表?

回答

1

用戶has_many :matching_profiles, :through => :expertises, class_name => "Profile", :source => :profiles

那麼你可以做@user.matching_profiles找到所有的有輪廓匹配專長的

你還必須有

has_many :profiles, :through => :expertise_profiles, :source => :profile在專業類

has_many :expertises, :through => :user_expertises, :source => :expertise在用戶類別

+0

如果你想顯示pro匹配用戶各有專長的文件,那麼你會在控制器希望這個 '@expertises = @ user.expertises.includes(:型材)' 然後在視圖中遍歷@expertises '<%@專業知識%> <%= expertise.name%> <%= expertise.profiles.map(:名稱)。加入( 「」)%> %< end %> 每個模型 – mgidea

+0

我假設名稱列得到一個錯誤'找不到關聯:expertises_recruiter在模型Recruiter' –

+0

也我會把這個'@ user.matching_profiles'這可能是爲什麼我得到一個錯誤 –