2016-03-28 109 views
0

我在實現書籤功能時遇到問題。我想查詢與當前用戶ID匹配的書籤表中的所有技巧。問題是我無法得到查詢來提取真實技術的屬性。我不斷收到法 「UID」 失蹤在belongs_to關聯中查詢foreing_key。 Rails

這是與查詢

def index 
@technique = Bookmark.joins(:technique).where(user_id: current_user) 
end 

的書籤模型

class Bookmark < ActiveRecord::Base 
belongs_to :user 
belongs_to :technique 
end 

用戶模型

class User < ActiveRecord::Base 
has_many :bookmarks 
end 

我相信他們的控制器是我的查詢問題,但我卡住了。

+0

是'Technique'一個模型,'belongs_to的:user'? –

回答

1

當你做Bookmark.joins(:technique).where(user_id: current_user),你收集bookmarks,而不是techniques

如果你想techniques,你可以做到以下幾點:因爲current_userwhere子句中所要求的正確類型的不

Bookmark.joins(:technique).where(user_id: current_user.id).collect(&:technique)

+0

完美謝謝! – nharvey27

0

查詢拋出一個錯誤。您需要指定當前用戶的ID。此外,您的查詢將導致收集:bookmarks而不是techniques

對此的解決方案是執行joins查詢和獲取的技術匹配的集合:

def index 
@technique = Bookmark.joins(:technique).where(user_id: current_user.id).collect(&:technique) 
end