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.
ruby-on-rails ranking
add a comment |
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.
ruby-on-rails ranking
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
add a comment |
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.
ruby-on-rails ranking
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
ruby-on-rails ranking
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
add a comment |
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
add a comment |
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.
That's exactly what I was looking for. Thank you so much.
– Johnny
Nov 22 at 16:03
add a comment |
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.
That's exactly what I was looking for. Thank you so much.
– Johnny
Nov 22 at 16:03
add a comment |
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.
That's exactly what I was looking for. Thank you so much.
– Johnny
Nov 22 at 16:03
add a comment |
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.
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.
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
add a comment |
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
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.
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.
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%2f53434162%2fcustom-ranking-based-on-multiple-criteria-in-rails%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
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