Rails use Boolean similar to counter_cache? -


a miniatures model has many collections. users can have , vote best collection version of miniature. votes in model called imagevotes update counter_cache attribute in collections model.

what want flag collections ranked first miniature gold, rank 2nd, 3rd , 4th silver. realise can on miniature model selecting @miniature.collection.first, able store store vote-count in counter_cache display total number of golds or silvers 1 user.

is there way each model have boolean fields called gold , silver updated new votes cast in same way counter_cache updated?

any pointers , further reading appreciated.

update:

it occurs me done sort of second index column. vote_position column if will, updated number "1" record highest counter_cache number , ascended there. use @miniature.collection.where(:vote_position => "1") or similar. perhaps more ideal?

as seems me need implement method in miniature model:

def set_gold_and_silver   top_collections = self.collections.order("imagevotes_count desc").limit(4)   gold = top_collections.shift    gold.update_attribute :is_gold, true if gold   top_collections.each {|s| s.update_attribute :is_silver, true} end 

after can add after_create filter of imagevotes model:

class imagevotes < activerecord::base   after_create :set_gold_and_silver    def set_gold_and_silver     self.miniature.set_gold_and_silver   end end 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -