0

Rails 4.0 アプリケーションで act_as_commentable_with_threading 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: "my@email.com", 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

ドキュメントをチェックアウトしましたが、何が間違っているのかわかりません。コメントと通知の間にまだ関連付けがありますか? 宝石はすでにそれを行っていると思います。

どんな助けでも大歓迎です。

4

1 に答える 1

1

build_from実際にコメントをデータベースに保存していないときに使用する場合。User代わりに、モデルに基づいて構築しているだけです。

したがって、実行Comment.countするとデータベースにクエリが実行され、コメントが保存されていないため、結果はゼロになります。

データベースに永続化するには、ビルド後にcomment.saveまたはを呼び出す必要があります。comment.save!

役立つことを願っています

于 2015-01-02T20:20:34.477 に答える