vote - Rails: How to increment counter_cache? -


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::: solved :::

was able figure out counter_cache on vote model , use in topics. updated question below. once topic model had votes_count, had put in view , update counting in controllers. turned out pretty simple. finding information not, should others out, in confusing glory. go way way down answer.

cheers

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::edit:::

:::see below history:::

this edit original post. i'm close have, before try gem solution. used comment_cache on vote model don't know how increment cache.

class vote < activerecord::base     belongs_to :topic, :counter_cache => true end 

and called topic.votes.size in view , shows count without active record enumerable.

<td><%= pluralize(topic.votes.size, "vote") %></td>         <td><%= button_to '+1', upvote_topic_path(topic), method: :post %></td>         <td><%= button_to '-1', downvote_topic_path(topic), method: :post %></td> 

index.html.erb

listing topics  title    test    1 vote  [+1] [-1]   delete  new topic 

but comment_cache doesn't allow negative numbers. i'm still using create/destroy on records rather adding , subtracting number count. how increment , decrement? methods i'm missing values. makes sense @topic.votes.last.destroy fails in there no more votes destroy tally cannot less zero.

how setup i'm incrementing/decrementing comment_cache value such negative numbers possible? tried @topic.votes.increment , @topic.votes.increment_counter ask 2 params i'm not sure use. tried

@topic.increment_counter(:votes_count, params[:id])

but no luck.

nomethoderror in topicscontroller#upvote undefined method `increment_counter' #<topic:0x007f87d974a4b0>  extracted source (around line #433): 431 432 433 434 435 436        else         match = match_attribute_method?(method.to_s)         match ? attribute_missing(match, *args, &block) : super       end     end 

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::history below line:::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

revisiting rails again , i'm trying simple vote counter on topics. want increment/decrement tally (integer) column in votes record.

topic has_may :votes, , vote belongs_to :topic

in topics_controller.rb i've used

def upvote     @topic = topic.find(params[:id])     @topic.votes.increment_counter(:tally, params[:id])     redirect_to(topics_path)   end    def downvote     @topic = topic.find(params[:id])     @topic.votes.decrement_counter(:tally, params[:id])     redirect_to(topics_path)   end 

but interesting activerecord key instead of value in view instead of count of votes.

index.html.erb:

from view <td><%= pluralize(topic.votes(:tally), "vote") %></td> <td><%= button_to '+1', upvote_topic_path(topic), method: :post %></td> <td><%= button_to '-1', downvote_topic_path(topic), method: :post %></td>  result of view     test    #<vote::activerecord_associations_collectionproxy:0x007fbb16451f68> votes             delete 

from rails console though increment doesn't show up:

  2.1.5 :022 > vote.first   vote load (0.5ms)  select  "votes".* "votes"  order "votes"."id" asc limit 1  => nil   2.1.5 :023 > vote  => vote(id: integer, topic_id: integer, created_at: datetime, updated_at: datetime, tally: integer)  

i think i've setup routes.rb ok double i'm not sure about

rails.application.routes.draw   resources :topics     member       post 'upvote'       post 'downvote'     end   end    root 'topics#index'   end 

anyway think it's either wrong method increment or i'm missing. appreciated. cheers

do have user model working votes? if so, there pretty awesome gem handles called acts_as_votable - https://github.com/ryanto/acts_as_votable

you bundle gem, run migrations, add acts_as_votable topics model, , acts_as_voter user model, can use load of helper methods.

topicscontroller.rb

    def upvote         @topic.upvote_by current_user         redirect_to :back     end      def downvote         @topic.downvote_by current_user         redirect_to :back     end 

routes.rb

    resources :topics       member         "upvote", to: "topics#upvote"         "downvote", to: "topics#downvote"       end     end 

/topics/show.html.erb

    <%= link_to upvote_topic_path(@topic), method: :get  %>     <%= link_to downvote_topic_path(@topic), method: :get  %>      <%= pluralize(@topic.get_upvotes.size, 'vote') %> 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -