如何将 chartkick line_chart 与 Rails 中的依赖模型一起使用?
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) %>
我有两个具有 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) %>