On 1 July 2015 at 09:33, Federicko <fedomain@gmail.com> wrote:
> Yeah, I will check that gem out.
> I basically wanted to do this to learn the ways of the Rails :p
In that case I would have two member methods rankup and rankdown that
do everything, including saving, all wrapped in a transaction.
So controller code is just something like
@this_article = Article.find(params[:id])
if params[:rank] == 'up'
@this_article.rankup
elsif params[:rank] == 'down'
@this_article.rankdown
else
# cope with this error appropriately
end
Then you have moved everything that needs to know how ranking works
into the model, while the controller deals with parameters.
Within the model the up/down might be performed using a class method
swap_rank( article_1, article_2 ) with rankup and rankdown finding the
articles affected.
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/acd8dac8-a5a2-4b8f-abfc-3e72ad12b255%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
--
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%3D0gLu0_YjKt0LOGm9xVL-UYaiM16dpYV94hdnehrYZBX66bA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
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/acd8dac8-a5a2-4b8f-abfc-3e72ad12b255%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On 1 July 2015 at 05:06, Federicko <fedomain@gmail.com> wrote:
> Hi All,
>
> First of all, let me quickly clarify the meaning of ranking here.
> All I'm doing is move an article up or down in the display on the index
> file.
>
> Quick example. Below is a sample of the Articles table:
>
> ID title ranking
> 5 Rails is cool 1
> 6 Programming is fun 2
> 7 How to install Rails 3
> 8 Macbook Pro features 4
>
> If a user click on article ID 7 to move it up, this is what the system
> should do:
>
> Change the ranking for article ID 7 to 2
> Change the ranking for article ID 6 to 3
>
> Basically, swap them around.
>
> So as you can see, when a user click on an article, the system needs to
> modify 2 records in the db.
>
> Now, if I want to move the self.save to the controller, the best way I can
> think of the implement this is as follows:
Don't move it to the controller. If it affects multiple records in
the db then put it in the model. In addition wrap the complete
operation in a transaction so that you are guaranteed to do both or
none, otherwise you will end up with the db in an illegal state.
However, if you are effectively maintaining an ordered list then I
suggest looking at the gem acts_as_list which will take much of the
hard work out of this for you.
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%3D0gLu2OUKTJJOYTW%3D5rd4vezX_zS7r%2B2%2ByS3%2B5EFsFqf8eoQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.