【问题标题】:acts_as_commentable_with_threading gem is not working well when I use build_from helper in my Rails application.当我在 Rails 应用程序中使用 build_from 助手时,acts_as_commentable_with_threading gem 无法正常工作。
【发布时间】:2015-01-02 20:09:47
【问题描述】:

我在 Rails 4.0 应用程序中使用acts_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

我查看了文档,但我不知道我做错了什么?评论和通知之间还有关联吗?我认为宝石已经这样做了。

任何帮助将不胜感激。

【问题讨论】:

  • 好的,看来我必须将结果保存在实例中并保存!它。

标签: ruby-on-rails ruby-on-rails-4 acts-as-commentable


【解决方案1】:

当您使用build_from 时,实际上并没有将评论保存到数据库中;相反,您只是根据您的 User 模型构建它。

因此,当您执行 Comment.count 时,您正在查询您的数据库,并且由于未保存评论,因此它返回零个结果。

您必须在构建后调用comment.savecomment.save! 才能将其持久化到数据库中。

希望对你有帮助

【讨论】:

  • 哦!现在我看到你已经解决了。对不起,我不够快:(
猜你喜欢
  • 2018-06-19
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
相关资源
最近更新 更多