Blacklist model in Rails 4

579 views Asked by At

I am using Act_as_votable to implement like/dislike voting system. It works just perfect.

But now I am facing problem to blacklist exact that item that has received at least 30 downvotes.

I have model Advertisement. That has column in_blacklist with default value false. I added in_blacklist field as permitted in Advertisement controller. I have tried this so far.

In view:

<%= link_to "Like", like_advertisement_path(@advertisement), method: :put %> <%= @advertisement.get_likes.size %>)
(<%= link_to "Dislike", dislike_advertisement_path(@advertisement), method: :put %> <%= @advertisement.get_dislikes.size %>)

In controller:

def downvote
  @advertisement.downvote_from current_user
flash[:notice] = 'Downvote added.' if @advertisement.vote_registered?
  if @advertisement.get_dislikes.size ==  30

    @advertisement.in_blacklist = true
     flash[:notice] = "#{@advertisement.name } added to blacklist. Information sent to #{@advertisement.user.email } "
    respond_with(@advertisement)
  else
  respond_with(@advertisement)
  end
end

So when I hit 30th dislike, there is no error messages. And when I check if the boolean has changed, but no it is still false.

1

There are 1 answers

0
Rubyrider On BEST ANSWER

You forgot to save the object.

def downvote
  @advertisement.downvote_from current_user
flash[:notice] = 'Downvote added.' if @advertisement.vote_registered?
  if @advertisement.get_dislikes.size ==  30

    @advertisement.in_blacklist = true
    @advertisement.save # there you go!
     flash[:notice] = "#{@advertisement.name } added to blacklist. Information sent to #{@advertisement.user.email } "
    respond_with(@advertisement)
  else
  respond_with(@advertisement)
  end
end