Ordering activerecord relationship from a specific starting value
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
add a comment |
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
add a comment |
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
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
ruby-on-rails
edited Nov 20 '18 at 14:11
Maxence
asked Nov 20 '18 at 13:54
MaxenceMaxence
6591616
6591616
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
edited Nov 20 '18 at 14:38
answered Nov 20 '18 at 14:33
abaxabax
66738
66738
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 20 '18 at 14:38
xlembourasxlembouras
6,80442435
6,80442435
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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