Ruby on rails 在非标准上使用rails计数器缓存有很多:通过
所以使用has_多:through关联的标准方法是使用活动记录关联指南中的医生预约患者模型,对吗?它基本上是一个更详细的HABTM,有两个模型有一个has\u many:through和一个connector模型有两个属于。现在,在我当前的项目中,我有一个如下的模型结构:Ruby on rails 在非标准上使用rails计数器缓存有很多:通过,ruby-on-rails,activerecord,ruby-on-rails-4,counter,model-associations,Ruby On Rails,Activerecord,Ruby On Rails 4,Counter,Model Associations,所以使用has_多:through关联的标准方法是使用活动记录关联指南中的医生预约患者模型,对吗?它基本上是一个更详细的HABTM,有两个模型有一个has\u many:through和一个connector模型有两个属于。现在,在我当前的项目中,我有一个如下的模型结构: class Cart < ActiveRecord::Base has_many :line_items, through: line_item_groups has_many :line_item_groups
class Cart < ActiveRecord::Base
has_many :line_items, through: line_item_groups
has_many :line_item_groups
end
class LineItemGroup < ActiveRecord::Base
belongs_to :cart
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :line_item_group
has_one :cart, through: line_item_group
end
class购物车
很好。但是现在我想要一个购物车上的行\u项目\u计数,却不知道应该在哪里添加counter\u cache属性
感谢您的帮助:-)首先在carts表中添加行项目计数字段,然后在行项目模型中添加
class LineItem < ActiveRecord::Base
before_create :increment_counter
before_destroy :decrement_counter
def increment_counter
Cart.increment_counter(:line_item_count, cart.id)
end
def decrement_counter
Cart.decrement_counter(:line_item_count, cart.id)
end
end
class LineItem
我没有尝试过,但我认为它会解决您的问题。我认为您无法通过rails实现,您必须自己实现它。