How to write this MySQL query using ActiveRecord
I'm working with a Rails 5 project and have a SQL query that looks like the following:
SELECT foo1.*
FROM foos foo1
WHERE foo1.created_at =
( SELECT MIN(foo2.created_at) FROM foos foo2 WHERE foo2.user_id = foo1.user_id );
The model here is named Foo
and the underlying table is named foos
. I want to write a method that'll basically give me one record per user_id with the earliest created_at timestamp which the SQL query above will solve. I just want to write it using ActiveRecord.
ruby-on-rails ruby-on-rails-5
add a comment |
I'm working with a Rails 5 project and have a SQL query that looks like the following:
SELECT foo1.*
FROM foos foo1
WHERE foo1.created_at =
( SELECT MIN(foo2.created_at) FROM foos foo2 WHERE foo2.user_id = foo1.user_id );
The model here is named Foo
and the underlying table is named foos
. I want to write a method that'll basically give me one record per user_id with the earliest created_at timestamp which the SQL query above will solve. I just want to write it using ActiveRecord.
ruby-on-rails ruby-on-rails-5
add a comment |
I'm working with a Rails 5 project and have a SQL query that looks like the following:
SELECT foo1.*
FROM foos foo1
WHERE foo1.created_at =
( SELECT MIN(foo2.created_at) FROM foos foo2 WHERE foo2.user_id = foo1.user_id );
The model here is named Foo
and the underlying table is named foos
. I want to write a method that'll basically give me one record per user_id with the earliest created_at timestamp which the SQL query above will solve. I just want to write it using ActiveRecord.
ruby-on-rails ruby-on-rails-5
I'm working with a Rails 5 project and have a SQL query that looks like the following:
SELECT foo1.*
FROM foos foo1
WHERE foo1.created_at =
( SELECT MIN(foo2.created_at) FROM foos foo2 WHERE foo2.user_id = foo1.user_id );
The model here is named Foo
and the underlying table is named foos
. I want to write a method that'll basically give me one record per user_id with the earliest created_at timestamp which the SQL query above will solve. I just want to write it using ActiveRecord.
ruby-on-rails ruby-on-rails-5
ruby-on-rails ruby-on-rails-5
asked Nov 23 '18 at 21:12
randombitsrandombits
11.3k55179349
11.3k55179349
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The initial query you have doesn't necessarily give you one record per user_id
.
Here's an example:
# created_at1 < created_at2
foo1 = Foo.create!(user_id: 1, created_at: created_at1)
foo2 = Foo.create!(user_id: 1, created_at: created_at2)
foo3 = Foo.create!(user_id: 2, created_at: created_at2)
Your query will select the minimum created_at
for each user_id
, so it will get created_at1
from foo1
and created_at2
from foo3
. But since foo2
and foo3
share the same created_at
, all three records will be returned.
A better way to select the records would be (For Postgres):
Foo.select('DISTINCT ON ("user_id") *').order(:user_id, created_at: :asc)
add a comment |
Check with following,
Foo.group(:user_id).having('MIN(created_at) >= created_at')
You have to check for <=
or >=
in above.
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%2f53452987%2fhow-to-write-this-mysql-query-using-activerecord%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
The initial query you have doesn't necessarily give you one record per user_id
.
Here's an example:
# created_at1 < created_at2
foo1 = Foo.create!(user_id: 1, created_at: created_at1)
foo2 = Foo.create!(user_id: 1, created_at: created_at2)
foo3 = Foo.create!(user_id: 2, created_at: created_at2)
Your query will select the minimum created_at
for each user_id
, so it will get created_at1
from foo1
and created_at2
from foo3
. But since foo2
and foo3
share the same created_at
, all three records will be returned.
A better way to select the records would be (For Postgres):
Foo.select('DISTINCT ON ("user_id") *').order(:user_id, created_at: :asc)
add a comment |
The initial query you have doesn't necessarily give you one record per user_id
.
Here's an example:
# created_at1 < created_at2
foo1 = Foo.create!(user_id: 1, created_at: created_at1)
foo2 = Foo.create!(user_id: 1, created_at: created_at2)
foo3 = Foo.create!(user_id: 2, created_at: created_at2)
Your query will select the minimum created_at
for each user_id
, so it will get created_at1
from foo1
and created_at2
from foo3
. But since foo2
and foo3
share the same created_at
, all three records will be returned.
A better way to select the records would be (For Postgres):
Foo.select('DISTINCT ON ("user_id") *').order(:user_id, created_at: :asc)
add a comment |
The initial query you have doesn't necessarily give you one record per user_id
.
Here's an example:
# created_at1 < created_at2
foo1 = Foo.create!(user_id: 1, created_at: created_at1)
foo2 = Foo.create!(user_id: 1, created_at: created_at2)
foo3 = Foo.create!(user_id: 2, created_at: created_at2)
Your query will select the minimum created_at
for each user_id
, so it will get created_at1
from foo1
and created_at2
from foo3
. But since foo2
and foo3
share the same created_at
, all three records will be returned.
A better way to select the records would be (For Postgres):
Foo.select('DISTINCT ON ("user_id") *').order(:user_id, created_at: :asc)
The initial query you have doesn't necessarily give you one record per user_id
.
Here's an example:
# created_at1 < created_at2
foo1 = Foo.create!(user_id: 1, created_at: created_at1)
foo2 = Foo.create!(user_id: 1, created_at: created_at2)
foo3 = Foo.create!(user_id: 2, created_at: created_at2)
Your query will select the minimum created_at
for each user_id
, so it will get created_at1
from foo1
and created_at2
from foo3
. But since foo2
and foo3
share the same created_at
, all three records will be returned.
A better way to select the records would be (For Postgres):
Foo.select('DISTINCT ON ("user_id") *').order(:user_id, created_at: :asc)
answered Nov 23 '18 at 22:24
AbMAbM
4,92521423
4,92521423
add a comment |
add a comment |
Check with following,
Foo.group(:user_id).having('MIN(created_at) >= created_at')
You have to check for <=
or >=
in above.
add a comment |
Check with following,
Foo.group(:user_id).having('MIN(created_at) >= created_at')
You have to check for <=
or >=
in above.
add a comment |
Check with following,
Foo.group(:user_id).having('MIN(created_at) >= created_at')
You have to check for <=
or >=
in above.
Check with following,
Foo.group(:user_id).having('MIN(created_at) >= created_at')
You have to check for <=
or >=
in above.
answered Nov 24 '18 at 4:50
rayray
1,505219
1,505219
add a comment |
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%2f53452987%2fhow-to-write-this-mysql-query-using-activerecord%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