Hi Colin,

I have done that but slightly differently as the 'this' variable was giving 
me an error.

Controller didn't change. It still just calls the rankup or rankdown 
methods in the model. 
Here's the code for that:

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

Then in my model I have 3 methods:

def rankup

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

  swap_rank(@affected_article)

end

def rankdown

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

  swap_rank(@affected_article)
end

private

def swap_rank(affected_article)

  @current_ranking = self.ranking

  self.ranking = affected_article.ranking
  affected_article.ranking = @current_ranking

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

This way all the code sits in one method and there is no repeated code

-- 
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/6d9da33f-8401-4e4a-8a35-ea769c7efd66%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to