Ruby on Rails Monday, June 29, 2015

Hi All,

I have moved everything over to the model as suggested. Check it out :p

In the model, I created a Class method as well as the previous instance methods:

class Article < ActiveRecord::Base

def self.rank(id, rank)
  @this_article = find(id)

  if (rank == "up")
    @next_article = find_by(rank: @this_article.rank.to_i-1)

    @this_article.rankup
    @next_article.rankdown
  else
    @next_article = find_by(rank: @this_article.rank.to_i+1)

    @this_article.rankdown
    @next_article.rankup
  end
end

def rankup
  self.rank = self.rank - 1
  self.save
end

def rankdown
  self.rank = self.rank + 1
  self.save
end

end

 And now in my controller, I only have one action:

def rank
  Article.rank(params[:id], params[:rank])
  redirect_to articles_path
end

It is working beautifully so I'm happy about that. 
However, can anyone please review the code and let me know if it is correct in terms of OOP?
And if it is best practice for Rails.


Rails is awesome.

Cheers

--
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/d9630b45-6824-4ab6-9f11-62b677df039f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment