2015-01-02 13 views
0

我正在使用acts_as_commentable_with_threading gem與我的Rails 4.0應用程序。acts_as_commentable_with_threading當我在Rails應用程序中使用build_from助手時,gem不能很好地工作。

我有通知控制器:

class Notification < ActiveRecord::Base 
    acts_as_commentable 
    # ...... 
end 

現在使用build_from創建註釋不工作:

>> n = Notification.last 
    Notification Load (2.3ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1 
=> #<Notification id: 134, owner_id: 223, sender_id: 247, n_type: "SAVED_SEARCH", url: nil, content: "2219", read: false, created_at: "2015-01-02 19:52:54", updated_at: "2015-01-02 19:52:54", archived: false, short_url: "http://www.some_url.com"> 
>> u = User.last 
    User Load (1.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1 
=> #<User id: 252, email: "[email protected]", encrypted_password: "$2a$1...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, first_name: "eqbalosss", last_name: nil, created_at: "2015-01-01 20:16:52", updated_at: "2015-01-01 20:16:52", provider: nil, uid: nil> 
>> Comment.build_from(n, u.id, "test") 
=> #<Comment id: nil, commentable_id: 134, commentable_type: "Notification", title: nil, body: "test", subject: nil, user_id: 252, parent_id: nil, lft: nil, rgt: nil, created_at: nil, updated_at: nil> 
>> Comment.count 
    (1.0ms) SELECT COUNT(*) FROM "comments" 
=> 0 

我檢查了文件,我不知道我究竟做錯了什麼?我是否仍有評論和通知之間的關聯?我認爲寶石已經可以做到了。

任何幫助,將不勝感激。

+0

好吧,似乎我必須將結果保存在一個實例並保存!它。 –

回答

1

當您使用build_from時,實際上並沒有將註釋保存到數據庫中;相反,您只是根據您的User模型構建它。

因此,當您執行Comment.count時,您正在查詢數據庫,並且由於未保存評論,所以它返回零結果。

在構建它之後,您必須調用comment.savecomment.save!以將其保存到數據庫中。

我希望它有幫助

+0

我剛剛注意到了。感謝幫助的人。 –

+0

哦!現在我看到你已經解決了它。對不起,我速度不夠快:( –

+0

尚未解決,你幫忙過了;)乾杯 –

相關問題