Custom ranking based on multiple criteria in Rails











up vote
0
down vote

favorite












I need to rank records in a database table based on two columns:




availability_date




and




updated_at




They're both datetime-type columns, but the conditions for ranking are not linear in time. So, for example, in a scale of 0 to 10, 'updated_at' values:



between 14 and 30 days ago would get a 10,



whereas 13 to 9 days ago would get 9.5,



31 to 60 would get 9,



etc.



The same applies to the 'availability_date' column.



The final ranking/ordering result should be the average of values of both scales for each record.



I'm trying to write that logic in a controller and ideally wanted to avoid adding columns in the database to build the indexes.



I'm using Ruby 2.5.1 and Rails 5.2.



Does anyone know if that's feasible?



Thanks a lot in advance.










share|improve this question






















  • Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
    – LolWalid
    Nov 22 at 15:46










  • Yes, it should be in a single request. I'm using a MySQL 8.0 database.
    – Johnny
    Nov 22 at 15:52















up vote
0
down vote

favorite












I need to rank records in a database table based on two columns:




availability_date




and




updated_at




They're both datetime-type columns, but the conditions for ranking are not linear in time. So, for example, in a scale of 0 to 10, 'updated_at' values:



between 14 and 30 days ago would get a 10,



whereas 13 to 9 days ago would get 9.5,



31 to 60 would get 9,



etc.



The same applies to the 'availability_date' column.



The final ranking/ordering result should be the average of values of both scales for each record.



I'm trying to write that logic in a controller and ideally wanted to avoid adding columns in the database to build the indexes.



I'm using Ruby 2.5.1 and Rails 5.2.



Does anyone know if that's feasible?



Thanks a lot in advance.










share|improve this question






















  • Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
    – LolWalid
    Nov 22 at 15:46










  • Yes, it should be in a single request. I'm using a MySQL 8.0 database.
    – Johnny
    Nov 22 at 15:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to rank records in a database table based on two columns:




availability_date




and




updated_at




They're both datetime-type columns, but the conditions for ranking are not linear in time. So, for example, in a scale of 0 to 10, 'updated_at' values:



between 14 and 30 days ago would get a 10,



whereas 13 to 9 days ago would get 9.5,



31 to 60 would get 9,



etc.



The same applies to the 'availability_date' column.



The final ranking/ordering result should be the average of values of both scales for each record.



I'm trying to write that logic in a controller and ideally wanted to avoid adding columns in the database to build the indexes.



I'm using Ruby 2.5.1 and Rails 5.2.



Does anyone know if that's feasible?



Thanks a lot in advance.










share|improve this question













I need to rank records in a database table based on two columns:




availability_date




and




updated_at




They're both datetime-type columns, but the conditions for ranking are not linear in time. So, for example, in a scale of 0 to 10, 'updated_at' values:



between 14 and 30 days ago would get a 10,



whereas 13 to 9 days ago would get 9.5,



31 to 60 would get 9,



etc.



The same applies to the 'availability_date' column.



The final ranking/ordering result should be the average of values of both scales for each record.



I'm trying to write that logic in a controller and ideally wanted to avoid adding columns in the database to build the indexes.



I'm using Ruby 2.5.1 and Rails 5.2.



Does anyone know if that's feasible?



Thanks a lot in advance.







ruby-on-rails ranking






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 15:30









Johnny

155




155












  • Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
    – LolWalid
    Nov 22 at 15:46










  • Yes, it should be in a single request. I'm using a MySQL 8.0 database.
    – Johnny
    Nov 22 at 15:52


















  • Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
    – LolWalid
    Nov 22 at 15:46










  • Yes, it should be in a single request. I'm using a MySQL 8.0 database.
    – Johnny
    Nov 22 at 15:52
















Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
– LolWalid
Nov 22 at 15:46




Do you want to do it in a single SQL/NON SQL request ? If so what database are you using ?
– LolWalid
Nov 22 at 15:46












Yes, it should be in a single request. I'm using a MySQL 8.0 database.
– Johnny
Nov 22 at 15:52




Yes, it should be in a single request. I'm using a MySQL 8.0 database.
– Johnny
Nov 22 at 15:52












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










As your question currently stands, of course it is feasible. Try using sort_by, where you give a block specifying how to sort your data.



In this block you should write exactly the logic you are stating: translate the date to a value depending on how old it is and getting the average of those two values.



I can imagine something like:



def date_to_val(date)
days_ago = (Time.now - date).to_i / 1.day
return 10 if days_ago.between?(14, 30)
return 9.5 if days_ago.between?(9, 13)
return 9 if days_ago.between?(31, 60)
# and so on
end

def my_controller_action
@records = SomeClass.where(some_attr: some_val)
ordered = @records.sort_by do |e|
[
date_to_val(e.created_at),
date_to_val(e.availability_date)
].sum / 2.0
end
# do something with ordered
end


Now, if you want to do it purely with SQL queries, then that is another question.






share|improve this answer





















  • That's exactly what I was looking for. Thank you so much.
    – Johnny
    Nov 22 at 16:03











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',
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%2f53434162%2fcustom-ranking-based-on-multiple-criteria-in-rails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










As your question currently stands, of course it is feasible. Try using sort_by, where you give a block specifying how to sort your data.



In this block you should write exactly the logic you are stating: translate the date to a value depending on how old it is and getting the average of those two values.



I can imagine something like:



def date_to_val(date)
days_ago = (Time.now - date).to_i / 1.day
return 10 if days_ago.between?(14, 30)
return 9.5 if days_ago.between?(9, 13)
return 9 if days_ago.between?(31, 60)
# and so on
end

def my_controller_action
@records = SomeClass.where(some_attr: some_val)
ordered = @records.sort_by do |e|
[
date_to_val(e.created_at),
date_to_val(e.availability_date)
].sum / 2.0
end
# do something with ordered
end


Now, if you want to do it purely with SQL queries, then that is another question.






share|improve this answer





















  • That's exactly what I was looking for. Thank you so much.
    – Johnny
    Nov 22 at 16:03















up vote
2
down vote



accepted










As your question currently stands, of course it is feasible. Try using sort_by, where you give a block specifying how to sort your data.



In this block you should write exactly the logic you are stating: translate the date to a value depending on how old it is and getting the average of those two values.



I can imagine something like:



def date_to_val(date)
days_ago = (Time.now - date).to_i / 1.day
return 10 if days_ago.between?(14, 30)
return 9.5 if days_ago.between?(9, 13)
return 9 if days_ago.between?(31, 60)
# and so on
end

def my_controller_action
@records = SomeClass.where(some_attr: some_val)
ordered = @records.sort_by do |e|
[
date_to_val(e.created_at),
date_to_val(e.availability_date)
].sum / 2.0
end
# do something with ordered
end


Now, if you want to do it purely with SQL queries, then that is another question.






share|improve this answer





















  • That's exactly what I was looking for. Thank you so much.
    – Johnny
    Nov 22 at 16:03













up vote
2
down vote



accepted







up vote
2
down vote



accepted






As your question currently stands, of course it is feasible. Try using sort_by, where you give a block specifying how to sort your data.



In this block you should write exactly the logic you are stating: translate the date to a value depending on how old it is and getting the average of those two values.



I can imagine something like:



def date_to_val(date)
days_ago = (Time.now - date).to_i / 1.day
return 10 if days_ago.between?(14, 30)
return 9.5 if days_ago.between?(9, 13)
return 9 if days_ago.between?(31, 60)
# and so on
end

def my_controller_action
@records = SomeClass.where(some_attr: some_val)
ordered = @records.sort_by do |e|
[
date_to_val(e.created_at),
date_to_val(e.availability_date)
].sum / 2.0
end
# do something with ordered
end


Now, if you want to do it purely with SQL queries, then that is another question.






share|improve this answer












As your question currently stands, of course it is feasible. Try using sort_by, where you give a block specifying how to sort your data.



In this block you should write exactly the logic you are stating: translate the date to a value depending on how old it is and getting the average of those two values.



I can imagine something like:



def date_to_val(date)
days_ago = (Time.now - date).to_i / 1.day
return 10 if days_ago.between?(14, 30)
return 9.5 if days_ago.between?(9, 13)
return 9 if days_ago.between?(31, 60)
# and so on
end

def my_controller_action
@records = SomeClass.where(some_attr: some_val)
ordered = @records.sort_by do |e|
[
date_to_val(e.created_at),
date_to_val(e.availability_date)
].sum / 2.0
end
# do something with ordered
end


Now, if you want to do it purely with SQL queries, then that is another question.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 15:52









byrdEmmanuel

807215




807215












  • That's exactly what I was looking for. Thank you so much.
    – Johnny
    Nov 22 at 16:03


















  • That's exactly what I was looking for. Thank you so much.
    – Johnny
    Nov 22 at 16:03
















That's exactly what I was looking for. Thank you so much.
– Johnny
Nov 22 at 16:03




That's exactly what I was looking for. Thank you so much.
– Johnny
Nov 22 at 16:03


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53434162%2fcustom-ranking-based-on-multiple-criteria-in-rails%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu