counter_cache rails a child creation should increment the count intwo different models based on cond
Posted
by aditi-syal
on Stack Overflow
See other posts from Stack Overflow
or by aditi-syal
Published on 2010-03-19T05:34:25Z
Indexed on
2010/03/19
5:41 UTC
Read the original article
Hit count: 485
Hi,
I have 3 models
Recommendation Job Qualification
Recommendation model has two fields as work_type and work_id(foreign key for job/qualification based on work_type as "J"/"Q")
I am facing problem in using counter_cache
I have done this in recommendation.rb
belongs_to :job , :counter_cache => true, :foreign_key => "work_id" belongs_to :qualification , :counter_cache => true, :foreign_key => "work_id"
and
in job and qualification model files
has_many :recommendations , :conditions => {:work_type => "J"}
has_many :recommendations , :conditions => {:work_type => "Q"}
Both Job and Qualification Models have a column as recommendations_count
The problem is every time an object of recommendation is created count is increased in the both the models
Please help me with this
Thanks
© Stack Overflow or respective owner