【问题标题】:conditions on count column using counter catch rails 4使用计数器挡轨 4 在计数柱上的条件
【发布时间】:2015-09-10 07:42:54
【问题描述】:

关系:

投票.rb

belongs_to :user
belongs_to :celebrity, counter_cache: true

名人.rb

belongs_to :user
belongs_to :category
has_many :votes

我需要在投票模型中使用类似的条件

belongs_to :celebrity, counter_cache: true, :conditions => ['votes.upvote = ? ', true]

谁能帮帮我

【问题讨论】:

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


【解决方案1】:

您可以阅读this 文章,该文章基本上建议在父对象中添加一个新列(用于带有条件的值),并在子模型更改时更新它。

您也可以使用counter_culture gem,它基于相同的理念构建,但内置了更多功能。

【讨论】:

  • pastie.org/10409446 你认为这对我有帮助吗?我需要删除名人表中的 votes_count 列吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-02
  • 2020-10-25
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多