2014-09-29 20 views
2

我正在使用Devise來管理用戶模型。獲取用戶使用Devise創建的所有貼子

這裏的用戶模型,user.rb

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 

    acts_as_voter 
    has_many :topics 

    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable   

end 

這裏的主題模式,topic.rb

class Topic < ActiveRecord::Base 
    acts_as_voteable 
    belongs_to :user 
end 

當我創建一個帖子,用戶ID存儲在 「USER_ID」主題專欄。我想要一個用戶個人資料頁面,該頁面應顯示他所發佈的所有帖子。在我的用戶的個人資料頁面視圖,我有,

<%= Topic.find_by_user_id(@user.id) %> 

我有相同的用戶做了兩個帖子,

Topic

但是當我嘗試獲取用戶發佈的所有信息中, SQL命令的1這樣的LIMIT自動查詢,

SQL

也正因爲如此,該視圖fetchin g只有1個用戶發表的帖子。我如何獲取所有帖子?

回答

4

find_by_attribute將始終只返回一個結果。您需要將代碼更改爲:

Topic.where(user_id: @user.id) 

甚至更​​好:

@user.topics 
相關問題