mysql - 事件记录搜索查询

标签 mysql ruby-on-rails activerecord

我正在尝试执行以下操作:

1) 从我的投票表中获取当前评论 ID 的所有投票(我已经完成了)

2) 从评论表中获取值“score”。两个事件记录查询几乎相同(我相信是正确的)。这是我的这两个表(投票和评论)的架构和我的评论 Controller 。

3)我需要在我的评论模型中定义的方法中使用“分数”的值。

ActiveRecord::Schema.define(version: 20131207224402) do

  create_table "comments", force: true do |t|
    t.integer  "user_id"
    t.integer  "question_id"
    t.text     "comment"
    t.integer  "upvotes"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "score"
  end

 create_table "votes", force: true do |t|
     t.integer  "comment_id"
     t.integer  "user_id"
     t.integer  "upvote"
     t.integer  "downvote"
     t.datetime "created_at"
     t.datetime "updated_at"
   end

end 

这是我的评论模型

class Comment < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
  has_many :votes


  def total_score

    total_votes = Vote.where(:comment_id => self.id)
    current_score = Comment.where(:id => self.id)    ##This is the query that is not working properly.

    testing = current_score.score

    total_score ||= 0
    up = total_votes.inject(testing) {|sum, v|
      v.upvote ||= 0
      sum + v.upvote
    }
    down = total_votes.inject(testing) {|sum,v|
      v.downvote ||= 0
      sum + v.downvote
    }

    up.to_i + down.to_i

  end

end

为什么我不能使用 .score 来获取查询返回的变量的分数值?

例如:current_score.score = '某个数字'

我需要这个数字来设置我的注入(inject)方法中的当前“总和”。

这是我收到的错误:

ActionView::Template::Error (undefined method `score' for #<ActiveRecord::Relation::ActiveRecord_Relation_Comment:0x007fbfcc7e9260>):
    1: json.array!(@comments) do |comment|
    2:   json.extract! comment, :user_id, :question_id, :comment, :upvotes, :id, :score, :created_at
    3:   json.url comment_url(comment, format: :json)
    4:   json.comment_score comment.total_score
    5: end
  app/models/comment.rb:14:in `total_score'
  app/views/comments/index.json.jbuilder:4:in `block in _app_views_comments_index_json_jbuilder__2724241289986061767_70230927807240'
  app/views/comments/index.json.jbuilder:1:in `_app_views_comments_index_json_jbuilder__2724241289986061767_70230927807240'


  Rendered /Users/ScottDAlessandro/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
  Rendered /Users/ScottDAlessandro/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /Users/ScottDAlessandro/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (13.2ms)

最佳答案

我认为你的问题是 current_score = Comment.where(:id => self.id) 返回一个数组。尝试将该代码更改为:

current_score = Comment.find(self.id)

这里是有关 findwhere 的 stackoverflow 讨论的链接: https://stackoverflow.com/a/9574674/2925963

关于mysql - 事件记录搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483922/

相关文章:

ruby-on-rails - rails : How do you handle dynamic attributes in the database?

ruby-on-rails - rails : delete belongs_to object when old object is replaced with new

mysql - 更新插入而不使用重复键 MYSQL

java - 我可以强制 liquibase 3.5.1 忽略旧版变更集校验和差异吗?

ruby-on-rails - Rails 中的嵌套路由是什么?

ruby-on-rails - 日志(开发和产品)和服务器输出不工作 rails 3

mysql - 纬度和经度,2 个索引还是 1 个复合索引?

mysql - Rails 3.1 和 mysql [致命] 分配内存失败

javascript - $ 未定义,在 Rails 中使用 Select2 插件时未捕获 ReferenceError

ruby-on-rails - ActiveRecord 连接 - 仅返回精确匹配