Ordering activerecord relationship from a specific starting value












0















Let's say I get a photo_id 34



This photo is part of a specific photographers asset current_photographer.photos.all : 1, 23, 24, 34, 78



How is it possible to get the relationship current_photographer.photos.all result starting by the specific photo ID provided => 34, 78, 1, 23, 24 ?



EDIT: I want to keep the general order of photos. Each photo is preceeded by the previously created photo.(this is for a photo carousel)










share|improve this question





























    0















    Let's say I get a photo_id 34



    This photo is part of a specific photographers asset current_photographer.photos.all : 1, 23, 24, 34, 78



    How is it possible to get the relationship current_photographer.photos.all result starting by the specific photo ID provided => 34, 78, 1, 23, 24 ?



    EDIT: I want to keep the general order of photos. Each photo is preceeded by the previously created photo.(this is for a photo carousel)










    share|improve this question



























      0












      0








      0








      Let's say I get a photo_id 34



      This photo is part of a specific photographers asset current_photographer.photos.all : 1, 23, 24, 34, 78



      How is it possible to get the relationship current_photographer.photos.all result starting by the specific photo ID provided => 34, 78, 1, 23, 24 ?



      EDIT: I want to keep the general order of photos. Each photo is preceeded by the previously created photo.(this is for a photo carousel)










      share|improve this question
















      Let's say I get a photo_id 34



      This photo is part of a specific photographers asset current_photographer.photos.all : 1, 23, 24, 34, 78



      How is it possible to get the relationship current_photographer.photos.all result starting by the specific photo ID provided => 34, 78, 1, 23, 24 ?



      EDIT: I want to keep the general order of photos. Each photo is preceeded by the previously created photo.(this is for a photo carousel)







      ruby-on-rails






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 14:11







      Maxence

















      asked Nov 20 '18 at 13:54









      MaxenceMaxence

      6451616




      6451616
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You could create two result sets, one for >= 34 and one for < 34, and then combine them.



          current_photographer.photos.where('id >= ?', 34) + current_photographer.photos.where('id < ?', 34)



          These could be scopes:



          scope :above_id, ->(id) { where('id >= ?', id) }
          scope :below_id, ->(id) { where('id < ?', id) }


          then: current_photographer.photos.above_id(34) + current_photographer.photos.below_id(34)



          or add a class method using the scopes:



          def self.above_below_id(id)
          above_id(id) + below_id(id)
          end





          share|improve this answer


























          • I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

            – Maxence
            Nov 20 '18 at 14:37



















          1














          I think you would be better implementing in the application space. I don't really see how it can be done in a meaningful way in the database level.



          You can fetch the records in their original order and then manipulate the result.



          current_photographer.photos.partition {|photo| photo.id < 34 }.reverse.flatten





          share|improve this answer
























          • Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

            – Maxence
            Nov 20 '18 at 14:44













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394571%2fordering-activerecord-relationship-from-a-specific-starting-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You could create two result sets, one for >= 34 and one for < 34, and then combine them.



          current_photographer.photos.where('id >= ?', 34) + current_photographer.photos.where('id < ?', 34)



          These could be scopes:



          scope :above_id, ->(id) { where('id >= ?', id) }
          scope :below_id, ->(id) { where('id < ?', id) }


          then: current_photographer.photos.above_id(34) + current_photographer.photos.below_id(34)



          or add a class method using the scopes:



          def self.above_below_id(id)
          above_id(id) + below_id(id)
          end





          share|improve this answer


























          • I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

            – Maxence
            Nov 20 '18 at 14:37
















          1














          You could create two result sets, one for >= 34 and one for < 34, and then combine them.



          current_photographer.photos.where('id >= ?', 34) + current_photographer.photos.where('id < ?', 34)



          These could be scopes:



          scope :above_id, ->(id) { where('id >= ?', id) }
          scope :below_id, ->(id) { where('id < ?', id) }


          then: current_photographer.photos.above_id(34) + current_photographer.photos.below_id(34)



          or add a class method using the scopes:



          def self.above_below_id(id)
          above_id(id) + below_id(id)
          end





          share|improve this answer


























          • I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

            – Maxence
            Nov 20 '18 at 14:37














          1












          1








          1







          You could create two result sets, one for >= 34 and one for < 34, and then combine them.



          current_photographer.photos.where('id >= ?', 34) + current_photographer.photos.where('id < ?', 34)



          These could be scopes:



          scope :above_id, ->(id) { where('id >= ?', id) }
          scope :below_id, ->(id) { where('id < ?', id) }


          then: current_photographer.photos.above_id(34) + current_photographer.photos.below_id(34)



          or add a class method using the scopes:



          def self.above_below_id(id)
          above_id(id) + below_id(id)
          end





          share|improve this answer















          You could create two result sets, one for >= 34 and one for < 34, and then combine them.



          current_photographer.photos.where('id >= ?', 34) + current_photographer.photos.where('id < ?', 34)



          These could be scopes:



          scope :above_id, ->(id) { where('id >= ?', id) }
          scope :below_id, ->(id) { where('id < ?', id) }


          then: current_photographer.photos.above_id(34) + current_photographer.photos.below_id(34)



          or add a class method using the scopes:



          def self.above_below_id(id)
          above_id(id) + below_id(id)
          end






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 14:38

























          answered Nov 20 '18 at 14:33









          abaxabax

          66938




          66938













          • I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

            – Maxence
            Nov 20 '18 at 14:37



















          • I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

            – Maxence
            Nov 20 '18 at 14:37

















          I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

          – Maxence
          Nov 20 '18 at 14:37





          I was focused on finding the solution with a single query.. But your solution definitely does the job. thanks

          – Maxence
          Nov 20 '18 at 14:37













          1














          I think you would be better implementing in the application space. I don't really see how it can be done in a meaningful way in the database level.



          You can fetch the records in their original order and then manipulate the result.



          current_photographer.photos.partition {|photo| photo.id < 34 }.reverse.flatten





          share|improve this answer
























          • Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

            – Maxence
            Nov 20 '18 at 14:44


















          1














          I think you would be better implementing in the application space. I don't really see how it can be done in a meaningful way in the database level.



          You can fetch the records in their original order and then manipulate the result.



          current_photographer.photos.partition {|photo| photo.id < 34 }.reverse.flatten





          share|improve this answer
























          • Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

            – Maxence
            Nov 20 '18 at 14:44
















          1












          1








          1







          I think you would be better implementing in the application space. I don't really see how it can be done in a meaningful way in the database level.



          You can fetch the records in their original order and then manipulate the result.



          current_photographer.photos.partition {|photo| photo.id < 34 }.reverse.flatten





          share|improve this answer













          I think you would be better implementing in the application space. I don't really see how it can be done in a meaningful way in the database level.



          You can fetch the records in their original order and then manipulate the result.



          current_photographer.photos.partition {|photo| photo.id < 34 }.reverse.flatten






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 14:38









          xlembourasxlembouras

          6,78442435




          6,78442435













          • Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

            – Maxence
            Nov 20 '18 at 14:44





















          • Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

            – Maxence
            Nov 20 '18 at 14:44



















          Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

          – Maxence
          Nov 20 '18 at 14:44







          Thanks a lot. Your solution does work in a single query. I have already accepted the other answer but yours is slightly cleaner. Manipulating a single query is the way to go.

          – Maxence
          Nov 20 '18 at 14:44




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394571%2fordering-activerecord-relationship-from-a-specific-starting-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules