我想创建Thumbs Up投票系统,但我不知道如何以最佳方式完成。
这是我的参赛作品#invest(控制/动作):
def vote
if Entry.where(id: params[:entry_id]).first && Vote.where(entry_id: params[:entry_id], user_id: @current_user.id).first.nil? # Check if entry with given entry_id exist && Check if user vote previously.
Vote.create(entry_id: params[:entry_id], user_id: @current_user.id) # Create vote
ActiveRecord::Base.connection.execute("UPDATE `entries` SET `entries`.`points` = `entries`.`points` + 1 WHERE `entries`.`id` = #{params[:entry_id].to_i}") # Update entry points count.
end
render nothing: true
end
我认为这不是最佳方式,因为这个动作有很多查询。这是查询日志:
Started GET "/vote/2" for 127.0.0.1 at 2012-02-20 16:20:01 +0100
Processing by EntriesController#vote as JS
Parameters: {"entry_id"=>"2"}
←[1m←[36mUser Load (1.0ms)←[0m ←[1mSELECT id, name FROM `users` WHERE `users`.`auth_token` = '6f1aa3b944d530a1d52c6f40bcb69398' LIMIT 1←[0m
←[1m←[35mEntry Load (1.0ms)←[0m SELECT `entries`.* FROM `entries` WHERE `entries`.`id` = 2 LIMIT 1
←[1m←[36mVote Load (0.0ms)←[0m ←[1mSELECT `votes`.* FROM `votes` WHERE `votes`.`entry_id` = 2 AND `votes`.`user_id` = 1 LIMIT 1←[0m
←[1m←[35m (0.0ms)←[0m BEGIN
←[1m←[36mSQL (0.0ms)←[0m ←[1mINSERT INTO `votes` (`entry_id`, `user_id`) VALUES (2, 1)←[0m
←[1m←[35m (54.0ms)←[0m COMMIT
←[1m←[36m (16.0ms)←[0m ←[1mUPDATE `entries` SET `entries`.`points` = `entries`.`points` + 1 WHERE `entries`.`id` = 2←[0m
Rendered text template (0.0ms)
Completed 200 OK in 115ms (Views: 1.0ms | ActiveRecord: 78.0ms)
任何人都知道如何以最佳方式做到这一点?
答案 0 :(得分:2)
可以清理控制器逻辑。如果它只是竖起大拇指,其中1票= 1点,你可以使用counter_cache来跟踪点数。如果您还需要大拇指,那么您可以使用update_counters代替。
<强> Entry.rb 强>
class Entry < ActiveRecord::Base
has_many :votes
end
<强> Vote.rb 强>
class Vote < ActiveRecord::Base
belongs_to :entry, :counter_cache => :points
belongs_to :user
end
<强> User.rb 强>
class User < ActiveRecord::Base
has_many :votes
end
<强> EntriesController#表决强>
def vote
Vote.find_or_create_by_entry_id_and_user_id(params[:entry_id], @current_user.id)
render :nothing
end
对于新投票,SQL日志是:
Vote Load (0.3ms) SELECT "votes".* FROM "votes" WHERE "votes"."entry_id" = 3 AND "votes"."user_id" = 1 LIMIT 1
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "votes" ("created_at", "entry_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Tue, 21 Feb 2012 16:51:54 UTC +00:00], ["entry_id", 3], ["updated_at", Tue, 21 Feb 2012 16:51:54 UTC +00:00], ["user_id", 1]]
Entry Load (0.2ms) SELECT "entries".* FROM "entries" WHERE "entries"."id" = 3 LIMIT 1
SQL (0.2ms) UPDATE "entries" SET "points" = COALESCE("points", 0) + 1 WHERE "entries"."id" = 3
(2.3ms) COMMIT
对于现有投票:
Vote Load (0.4ms) SELECT "votes".* FROM "votes" WHERE "votes"."entry_id" = 3 AND "votes"."user_id" = 1 LIMIT 1
=> #<Vote id: 7, user_id: 1, entry_id: 3, created_at: "2012-02-21 16:51:54", updated_at: "2012-02-21 16:51:54">
答案 1 :(得分:0)
看起来你的构造函数都在做自己的查询..
您可以通过在UI中调用一次的数据库过程中的所有检查和更新逻辑来优化一点......这至少可以节省您的网络往返次数。
否则 - 看看不同的构造方式,可能更聪明的是不加载最小量的每一点信息......
答案 2 :(得分:0)
您可以使用查询来检查条目是否存在以及用户是否未对该条件进行投票 像这样:
SELECT enrty.* FROM enrty LEFT JOIN votes ON enrty.voteId=votes.Id AND votes.UserId=userId And enrty.Id=enrtyId WHERE vote.id IS NULL
如果此查询返回一个enrty,则表示该条目存在且用户尚未对该enrty投票。
答案 3 :(得分:0)