2016-03-28 111 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