On 2 July 2015 at 12:04, Federicko <[email protected]> wrote:
> 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

Too much repeated code
How about
def rankup
  @affected_article = Article.find_by(ranking: ranking.to_i-1)
  adjust_ranks this, @affected_article
end

etc.

Colin

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuUF8k71m4Ju7uykn4T9JGTPFX0BsoCt6vC2CpzEPzK6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to