Ruby on Rails Thursday, July 2, 2015

Oh yeah, like this?

In controller:

def rank
  @this_article = Article.find(params[:id])

  if (params[:rank] == 'up')
    @this_article.rankup
  else
    @this_article.rankdown
  end

  redirect_to articles_path()
end

In the model:

def rankup()

  @affected_article = Article.find_by(ranking: ranking.to_i-1)
  self.ranking = self.ranking - 1
  @affected_article.ranking = @affected_article.ranking + 1

  Article.transaction do
    self.save
    @affected_article.save
  end
end

def rankdown()

  @affected_article = Article.find_by(ranking: ranking.to_i+1)

  self.ranking = self.ranking + 1
  @affected_article.ranking = @affected_article.ranking - 1

  Article.transaction do
    self.save
    @affected_article.save
  end
end


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cbe0acb3-09a1-4097-ba8f-007809dc721a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment