2

I'm using counter_culture to create survey applications the problem is each time I add citizen the count columns is not automatically update I have to go to console and run Citizen.counter_culture_fix_counts below is my model and controller for reference I'm using rails 4 and nested_attributes

thank you for help

model

class Familycard < ActiveRecord::Base
    has_many :citizens  , :dependent => :destroy
  accepts_nested_attributes_for :citizens, :allow_destroy => :true
end

class Citizen < ActiveRecord::Base
  belongs_to :familycard

  counter_culture :familycard, 
    :column_name => Proc.new { |model| "#{model.sex}_count"},
    :column_names => {
      ["citizens.sex = ? ", 'male'] => 'males_count',
      ["citizens.sex = ? ", 'female'] => 'females_count'
    }

  counter_culture :familycard

  counter_culture :familycard, 
    :column_name => Proc.new { |model| "#{model.job}_count"},
    :column_names => {
      ["citizens.job = ? ", 'Entrepreneur'] => 'Entrepreneurs_count',
      ["citizens.job = ? ", 'House wife'] => 'housewifes_count',
      ["citizens.job = ? ", 'Student'] => 'students_count',
      ["citizens.job = ? ", 'Veteran'] => 'veterans_count',
    }    

end

controller

class FamilycardController < ApplicationController
  def new
    @familycard = Familycard.new(:citizens => [Citizen.new])
  end

  def create
    @familycard = Familycard.new(familycard_params)
    if @familycard.save
      flash[:success] = "Data Saved" 
      redirect_to familycards_path 
    else
      render 'familycards/familycard_form'
    end
  end
widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • Where are you expecting for the counter to increment? `counter_culture` only increments records `after_commit` but in your controller you don't seem to save any citizens at all... – mrstif Oct 22 '15 at 19:03
  • I've the same issue, how should I pass multiple conditions like where `published = true and type = some_type` When I edit and change the type or publish the post after creation the count doesn't get updated. I'm assuming you've solved this issue, how? – lightsaber Jun 28 '17 at 14:25
  • I just added sample code for your question – widjajayd Jun 28 '17 at 15:28

1 Answers1

1

follow up some comments from my question, I have solved my problem above, and below are sample code for condition for the gem

 counter_culture :parent_model, :column_name => Proc.new {|child_model| 
    if child_model.published_condition == 'CONDITION 1'
      "condition1_count"
    elsif child_model.published_condition == 'CONDITION 2'
      "condition2_count"
    elsif child_model.published_condition == 'CONDITION 3' 
      "condition3_count"               
    end
    }, :column_names => {
      ["child_models.published_condition = ?", 'CONDITION 1']   => 'condition1_count',
      ["child_models.published_condition = ?", 'CONDITION 2'] => 'condition2_count',
      ["child_models.published_condition = ?", 'CONDITION 3'] => 'condition3_count'
     }

explanation:

  • parent_model has 3 fields to save the total number which are condition1_count,condition2_count and condition3_count
widjajayd
  • 6,090
  • 4
  • 30
  • 41