[英]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
當然, :value
是Position
實例而不是集合實例的字段,那么怎么可能通過關鍵字位置的分鍾數來顯示圖表?
根據我對問題的理解
<%= line_chart @keyword.positions.group(:created_at).count(:value) %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.