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
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'
}