【问题标题】:How to use chartkick line_chart's with dependent models in Rails?如何在 Rails 中将 chartkick line_chart 与依赖模型一起使用?
【发布时间】:2016-02-03 03:35:05
【问题描述】:

我有两个具有belongs_to 关系的模型:

class Keyword < ActiveRecord::Base
  has_many :positions
  validates :name, presence: true, uniqueness: { case_sensitive: false }
end

class Position < ActiveRecord::Base
  belongs_to :keyword
  validates :value, presence: true #this is an integer
  validates :keyword_id, presence: true
end

我想根据created_at 字段的关键字位置值的分钟数创建一个line_chart

所以我正在尝试类似的东西:

<%= line_chart @keyword.positions.value %>

我得到一个错误:

undefined method `value' for #<Position::ActiveRecord_Associations_CollectionProxy

当然,:valuePosition 实例的字段,而不是集合,那么如何按关键字位置的分钟数显示图表?

【问题讨论】:

  • ON 你要制作折线图的哪个属性?

标签: ruby-on-rails chartkick


【解决方案1】:

根据我对问题的理解

<%= line_chart @keyword.positions.group(:created_at).count(:value) %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多