为什么`unvote_by`不适用于act-as-votable gem中的范围投票?

时间:2015-06-29 07:01:55

标签: ruby-on-rails ruby-on-rails-4

我正在使用acts-as-votable,而我有Post没有选票:

[74] pry(main)> p.votes_for
=> []

然后我添加了vote_scope的简单投票,如下所示:

[75] pry(main)> p.vote_by voter: a, vote_scope: :inspired
   (0.5ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 1 AND "votes"."vote_scope" = 'inspired' AND "votes"."voter_type" = 'User'  [["votable_id", 83], ["votable_type", "Post"]]
   (0.2ms)  BEGIN
  SQL (2.2ms)  INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_scope", "vote_weight", "voter_id", "voter_type") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["created_at", "2015-06-29 06:53:17.838517"], ["updated_at", "2015-06-29 06:53:17.838517"], ["votable_id", 83], ["votable_type", "Post"], ["vote_flag", "t"], ["vote_scope", "inspired"], ["vote_weight", 1], ["voter_id", 1], ["voter_type", "User"]]
   (0.5ms)  COMMIT
=> true

这给了我这个:

[76] pry(main)> p.votes_for.reload
  ActsAsVotable::Vote Load (0.4ms)  SELECT "votes".* FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2  [["votable_id", 83], ["votable_type", "Post"]]
=> [#<ActsAsVotable::Vote id: 6, votable_id: 83, votable_type: "Post", voter_id: 1, voter_type: "User", vote_flag: true, vote_scope: "inspired", vote_weight: 1, created_at: "2015-06-29 06:53:17", updated_at: "2015-06-29 06:53:17">]

所有这一切看起来都不错。

然而,当我尝试unvote这样的记录时 - 它似乎经历了:

[78] pry(main)> p.unvote_by a
   (0.5ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 1 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'User'  [["votable_id", 83], ["votable_type", "Post"]]
=> true

除非我查看原帖,否则我仍然会看到投票:

[79] pry(main)> p.votes_for.reload
  ActsAsVotable::Vote Load (0.3ms)  SELECT "votes".* FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2  [["votable_id", 83], ["votable_type", "Post"]]
=> [#<ActsAsVotable::Vote id: 6, votable_id: 83, votable_type: "Post", voter_id: 1, voter_type: "User", vote_flag: true, vote_scope: "inspired", vote_weight: 1, created_at: "2015-06-29 06:53:17", updated_at: "2015-06-29 06:53:17">]
[80] pry(main)> p.votes_for
=> [#<ActsAsVotable::Vote id: 6, votable_id: 83, votable_type: "Post", voter_id: 1, voter_type: "User", vote_flag: true, vote_scope: "inspired", vote_weight: 1, created_at: "2015-06-29 06:53:17", updated_at: "2015-06-29 06:53:17">]
[81] pry(main)> a.id
=> 1

请注意,source code herethe tests表示这应该有效,所以我不确定发生了什么。

导致这种情况的原因是什么?

修改1

请注意,当我手动销毁投票时,会将其正确删除,如下所示:

[85] pry(main)> v = p.votes_for.last
=> #<ActsAsVotable::Vote id: 6, votable_id: 83, votable_type: "Post", voter_id: 1, voter_type: "User", vote_flag: true, vote_scope: "inspired", vote_weight: 1, created_at: "2015-06-29 06:53:17", updated_at: "2015-06-29 06:53:17">
[86] pry(main)> v.destroy
   (0.2ms)  BEGIN
  SQL (0.5ms)  DELETE FROM "votes" WHERE "votes"."id" = $1  [["id", 6]]
   (0.9ms)  COMMIT
=> #<ActsAsVotable::Vote id: 6, votable_id: 83, votable_type: "Post", voter_id: 1, voter_type: "User", vote_flag: true, vote_scope: "inspired", vote_weight: 1, created_at: "2015-06-29 06:53:17", updated_at: "2015-06-29 06:53:17">
[88] pry(main)> p.votes_for.reload
  ActsAsVotable::Vote Load (0.3ms)  SELECT "votes".* FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2  [["votable_id", 83], ["votable_type", "Post"]]
=> []

1 个答案:

答案 0 :(得分:0)

好的想出来了。

这样做的方法很简单:

p.unvote_by a, vote_scope: :inspired

这是整个成功的unvoting日志的外观:

[102] pry(main)> p.unvote_by a, vote_scope: :inspired
   (0.5ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 1 AND "votes"."vote_scope" = 'inspired' AND "votes"."voter_type" = 'User'  [["votable_id", 83], ["votable_type", "Post"]]
  ActsAsVotable::Vote Load (0.3ms)  SELECT "votes".* FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 1 AND "votes"."vote_scope" = 'inspired' AND "votes"."voter_type" = 'User'  [["votable_id", 83], ["votable_type", "Post"]]
   (0.2ms)  BEGIN
  SQL (0.2ms)  DELETE FROM "votes" WHERE "votes"."id" = $1  [["id", 8]]
   (0.4ms)  COMMIT
   (1.9ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2  [["votable_id", 83], ["votable_type", "Post"]]
=> true