简体   繁体   English

如何遍历Rails中的Associations?

[英]How do I iterate through Associations in Rails?

Ok, So I have a review model, and a votes model (Upvotes and Downvotes modeled through a single variable 'likes', If its true its an upvote, if its false its a downvote.). 好的,所以我有一个评论模型和一个投票模型(通过单个变量“ likes”对Upvotes和Downvotes进行建模,如果为true,则为upvote,如果为false,则为downvote。)。 The votes are a has_many relation, and are polymorphic (I hope to be able to use this on more than just reviews later on). 投票是一个has_many关系,并且是多态的(我希望能够在以后的评论中更多地使用它)。

So in the reviews model has the line 因此,在评论模型中

 has_many :votes, :as => :votable

And the votes model is defined like this 投票模型是这样定义的

class Vote < ActiveRecord::Base
  belongs_to :votable, :polymorphic => true
  belongs_to :user

  def upvote?
    like
  end

  def downvote?
    !like
  end
end

Now this is fine, I can upvote, downvote and remove votes just fine. 现在可以了,我可以投票,投票和删除选票。 But I'm trying to generate a score bassed on the upvotes and downvotes (Simply upvotes-downvotes for now). 但是,我正在尝试根据赞成票和反对票(目前仅支持upvotes-downvotes)生成分数。 To do this I added the following to the reviews model 为此,我在评论模型中添加了以下内容

def score
  up = 0
  down = 0
  self.votes.each do |v|
    if v.upvote?
      up += 1
    elsif v.downvote?
      down += 1
    end
  end
  up-down
end

However I always get 0 back as the answer. 但是我总是得到0作为答案。 The error is in the loop, as I can set the up or down variables to be what ever outside of it and they are passed through. 错误在循环中,因为我可以将up或down变量设置为超出该范围的变量,并通过它们。 Its starting to drive me insane, and I have no idea where im going wrong. 它开始让我发疯,而且我不知道我哪里出错了。

For starters it might be best to use the database to count the number of down and upvotes with a named scope. 对于初学者,最好使用数据库来计算具有指定作用域的下降和上升的次数。

Then once you have the total counts, you can subtract downvotes from upvotes. 然后,一旦有了总数,就可以从增票中减去减票。

For more info on scopes see: http://guides.rubyonrails.org/active_record_querying.html#scopes 有关范围的更多信息,请参见: http : //guides.rubyonrails.org/active_record_querying.html#scopes

If you are sure the error is in the loop then here's my best guess: 如果您确定错误在循环中,那么这是我的最佳猜测:

you have v.upvote? 你有v.upvote吗? as an if, and v.downvote? 如果和v.downvote? as an elsif. 作为elsif。

What happens if both of these are false? 如果这两个都是假的怎么办? I am pretty sure nothing will happen. 我很确定不会发生任何事情。 And therefore, 0 - 0 = 0 and you always get zero back. 因此,0-0 = 0,您总会得到零。 The loop is perfectly fine, if your v.upvote? 如果您的v.upvote? and v.downvote? 和v.downvote? are written correctly, or they are the correct values to begin with. 正确写入,或者它们是正确的开始值。

Try: 尝试:

if v.upvote?
 ...
else
 ...
end

Obviously you have those setup in that way for a reason, but you are possibly just zoning in on the wrong thing. 显然,您以这种方式进行设置是有原因的,但是您可能只是在错误的区域进行分区。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails 4-如何遍历这些参数? - Ruby on Rails 4 - How do I iterate through these params? Rails关联:如何通过多个自引用条件来限制/限定has_many:? - Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions? 如何通过belongs_to 关联链编写Rails finder 方法? - How do I write a Rails finder method through a chain of belongs_to associations? 如何在rails 4中搜索关联 - how to search through associations in rails 4 在视图中通过多步骤关联进行迭代的Rails方法是什么? - What is the Rails Way to iterate through multi-step associations in a view? 如何使用在Rails中创建的关联? - How do I use the associations that I create in Rails? 如何在Rails中构建数据结构,以便可以像这样遍历它 - How do I build a data structure in rails so that I can iterate through it like this Rails:创建新条目时如何遍历模型中的嵌套属性 - Rails: How do I iterate through nested attributes in model when creating new entries 如何以Rails方式创建具有其他关联的关联? - How do I create an association that has other associations in a Rails way? 如何在Rails 3中显示has_many关联的属性? - How do I show attributes for has_many associations in Rails 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM