I'm using acts_as_commentable_with_threading gem with my Rails 4.0 application.
I have Notification controller:
class Notification < ActiveRecord::Base
acts_as_commentable
# ......
end
Now using build_from to create a comment is not working:
>> 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
I checked out the documentation and I have no clue what am I doing wrong? do I still have association between Comment and Notification? I assume that the gem would do that already.
Any help would be appreciated.
When you use
build_fromwhen are not actually saving the comment into the database; instead you're just building it based on yourUsermodel.So, when you perform
Comment.countyou're querying your database and since the comment wasn't saved, it returns zero results.You have to call either
comment.saveorcomment.save!after building it in order to persist it to the database.I hope it helps