Ruby on Rails Monday, June 29, 2015

On 29 June 2015 at 21:27, Federicko <fedomain@gmail.com> wrote:
>> 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

Rather than having a class method rank() I think it would be better to
have a member method then in the controller do something like
this_article = Article.find(params[:id])
this_article.rank( params[:rank] )

Also don't forget to allow for the fact that there might not be an
article with the given id, so perhaps
this_article.rank (params[:rank]) if this_article

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 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/CAL%3D0gLssAGSx0Mq_S%2BPFeBOHZRJsNVBtsQweBcHcfB%2B0k6vxGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment