簡體   English   中英

如何在Rails中將chartkick line_chart與相關模型一起使用?

[英]How to use chartkick line_chart's with dependent models in Rails?

我有兩個模型之間具有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實例而不是集合實例的字段,那么怎么可能通過關鍵字位置的分鍾數來顯示圖表?

根據我對問題的理解

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM