Ruby on Rails Wednesday, July 1, 2015

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.

No comments:

Post a Comment