How can I join only one record on a has_many with criteria in Rails?
up vote
0
down vote
favorite
If I have the following models how can I return all reports a user has created, but only the "highest graded" report per player?
class Player < ApplicationRecord
has_many :reports
end
class Report < ApplicationRecord
belongs_to :author
belongs_to :grade
belongs_to :player
end
class Grade < ApplciationRecord
has_many :reports
end
class Author < ApplicationRecord
has_many :reports
end
Example Data:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
John Smith - David - 4
Thomas Li - David - 5
Mike Lee - Sean - 9
Mike Lee - Sean - 2
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Result I would like:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
Thomas Li - David - 5
Mike Lee - Sean - 9
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Currently I'm using the following:
Report.joins(:player).where(type: %w(spring fall))
I'm unsure how to filter out the "lower graded" records. If I need to include more information please let me know.
ruby-on-rails activerecord ruby-on-rails-5
add a comment |
up vote
0
down vote
favorite
If I have the following models how can I return all reports a user has created, but only the "highest graded" report per player?
class Player < ApplicationRecord
has_many :reports
end
class Report < ApplicationRecord
belongs_to :author
belongs_to :grade
belongs_to :player
end
class Grade < ApplciationRecord
has_many :reports
end
class Author < ApplicationRecord
has_many :reports
end
Example Data:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
John Smith - David - 4
Thomas Li - David - 5
Mike Lee - Sean - 9
Mike Lee - Sean - 2
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Result I would like:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
Thomas Li - David - 5
Mike Lee - Sean - 9
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Currently I'm using the following:
Report.joins(:player).where(type: %w(spring fall))
I'm unsure how to filter out the "lower graded" records. If I need to include more information please let me know.
ruby-on-rails activerecord ruby-on-rails-5
Do you really need a separate table forGrade
? It would a lot simpler if you just used an integer onreports
.
– max
Nov 21 at 20:07
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If I have the following models how can I return all reports a user has created, but only the "highest graded" report per player?
class Player < ApplicationRecord
has_many :reports
end
class Report < ApplicationRecord
belongs_to :author
belongs_to :grade
belongs_to :player
end
class Grade < ApplciationRecord
has_many :reports
end
class Author < ApplicationRecord
has_many :reports
end
Example Data:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
John Smith - David - 4
Thomas Li - David - 5
Mike Lee - Sean - 9
Mike Lee - Sean - 2
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Result I would like:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
Thomas Li - David - 5
Mike Lee - Sean - 9
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Currently I'm using the following:
Report.joins(:player).where(type: %w(spring fall))
I'm unsure how to filter out the "lower graded" records. If I need to include more information please let me know.
ruby-on-rails activerecord ruby-on-rails-5
If I have the following models how can I return all reports a user has created, but only the "highest graded" report per player?
class Player < ApplicationRecord
has_many :reports
end
class Report < ApplicationRecord
belongs_to :author
belongs_to :grade
belongs_to :player
end
class Grade < ApplciationRecord
has_many :reports
end
class Author < ApplicationRecord
has_many :reports
end
Example Data:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
John Smith - David - 4
Thomas Li - David - 5
Mike Lee - Sean - 9
Mike Lee - Sean - 2
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Result I would like:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
Thomas Li - David - 5
Mike Lee - Sean - 9
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
Currently I'm using the following:
Report.joins(:player).where(type: %w(spring fall))
I'm unsure how to filter out the "lower graded" records. If I need to include more information please let me know.
ruby-on-rails activerecord ruby-on-rails-5
ruby-on-rails activerecord ruby-on-rails-5
asked Nov 21 at 19:07
daveomcd
2,8721065106
2,8721065106
Do you really need a separate table forGrade
? It would a lot simpler if you just used an integer onreports
.
– max
Nov 21 at 20:07
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16
add a comment |
Do you really need a separate table forGrade
? It would a lot simpler if you just used an integer onreports
.
– max
Nov 21 at 20:07
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16
Do you really need a separate table for
Grade
? It would a lot simpler if you just used an integer on reports
.– max
Nov 21 at 20:07
Do you really need a separate table for
Grade
? It would a lot simpler if you just used an integer on reports
.– max
Nov 21 at 20:07
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
On Postgres you can use DISTINCT ON
:
class Report < ApplicationRecord
belongs_to :player
belongs_to :grade
belongs_to :author
def self.highest_graded
Report.select(%q{
DISTINCT ON(reports.player_id, reports.author_id)
grades.grade AS max_grade,
players.name AS player_name,
authors.name AS author_name,
reports.*
}).joins(:player, :grade, :author)
.order('reports.player_id, reports.author_id, grades.grade DESC')
end
end
<table>
<thead>
<tr>
<th>id</th>
<th>Player</th>
<th>Author</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% Report.highest_grade.each do |report| %>
<tr>
<td><%= report.id %></td>
<td><%= report.player_name %></td>
<td><%= report.author_name %></td>
<td><%= report.max_grade %></td>
</tr>
<% end %>
</tbody>
</table>
id Player Author Grade
1 John Smith David 5
3 Thomas Li David 5
4 Mike Lee Sean 9
6 Arnold Jackson Sean 5
7 Cathleen Miller Sean 7
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
On Postgres you can use DISTINCT ON
:
class Report < ApplicationRecord
belongs_to :player
belongs_to :grade
belongs_to :author
def self.highest_graded
Report.select(%q{
DISTINCT ON(reports.player_id, reports.author_id)
grades.grade AS max_grade,
players.name AS player_name,
authors.name AS author_name,
reports.*
}).joins(:player, :grade, :author)
.order('reports.player_id, reports.author_id, grades.grade DESC')
end
end
<table>
<thead>
<tr>
<th>id</th>
<th>Player</th>
<th>Author</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% Report.highest_grade.each do |report| %>
<tr>
<td><%= report.id %></td>
<td><%= report.player_name %></td>
<td><%= report.author_name %></td>
<td><%= report.max_grade %></td>
</tr>
<% end %>
</tbody>
</table>
id Player Author Grade
1 John Smith David 5
3 Thomas Li David 5
4 Mike Lee Sean 9
6 Arnold Jackson Sean 5
7 Cathleen Miller Sean 7
add a comment |
up vote
1
down vote
accepted
On Postgres you can use DISTINCT ON
:
class Report < ApplicationRecord
belongs_to :player
belongs_to :grade
belongs_to :author
def self.highest_graded
Report.select(%q{
DISTINCT ON(reports.player_id, reports.author_id)
grades.grade AS max_grade,
players.name AS player_name,
authors.name AS author_name,
reports.*
}).joins(:player, :grade, :author)
.order('reports.player_id, reports.author_id, grades.grade DESC')
end
end
<table>
<thead>
<tr>
<th>id</th>
<th>Player</th>
<th>Author</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% Report.highest_grade.each do |report| %>
<tr>
<td><%= report.id %></td>
<td><%= report.player_name %></td>
<td><%= report.author_name %></td>
<td><%= report.max_grade %></td>
</tr>
<% end %>
</tbody>
</table>
id Player Author Grade
1 John Smith David 5
3 Thomas Li David 5
4 Mike Lee Sean 9
6 Arnold Jackson Sean 5
7 Cathleen Miller Sean 7
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
On Postgres you can use DISTINCT ON
:
class Report < ApplicationRecord
belongs_to :player
belongs_to :grade
belongs_to :author
def self.highest_graded
Report.select(%q{
DISTINCT ON(reports.player_id, reports.author_id)
grades.grade AS max_grade,
players.name AS player_name,
authors.name AS author_name,
reports.*
}).joins(:player, :grade, :author)
.order('reports.player_id, reports.author_id, grades.grade DESC')
end
end
<table>
<thead>
<tr>
<th>id</th>
<th>Player</th>
<th>Author</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% Report.highest_grade.each do |report| %>
<tr>
<td><%= report.id %></td>
<td><%= report.player_name %></td>
<td><%= report.author_name %></td>
<td><%= report.max_grade %></td>
</tr>
<% end %>
</tbody>
</table>
id Player Author Grade
1 John Smith David 5
3 Thomas Li David 5
4 Mike Lee Sean 9
6 Arnold Jackson Sean 5
7 Cathleen Miller Sean 7
On Postgres you can use DISTINCT ON
:
class Report < ApplicationRecord
belongs_to :player
belongs_to :grade
belongs_to :author
def self.highest_graded
Report.select(%q{
DISTINCT ON(reports.player_id, reports.author_id)
grades.grade AS max_grade,
players.name AS player_name,
authors.name AS author_name,
reports.*
}).joins(:player, :grade, :author)
.order('reports.player_id, reports.author_id, grades.grade DESC')
end
end
<table>
<thead>
<tr>
<th>id</th>
<th>Player</th>
<th>Author</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% Report.highest_grade.each do |report| %>
<tr>
<td><%= report.id %></td>
<td><%= report.player_name %></td>
<td><%= report.author_name %></td>
<td><%= report.max_grade %></td>
</tr>
<% end %>
</tbody>
</table>
id Player Author Grade
1 John Smith David 5
3 Thomas Li David 5
4 Mike Lee Sean 9
6 Arnold Jackson Sean 5
7 Cathleen Miller Sean 7
answered Nov 21 at 21:25
max
44.1k856103
44.1k856103
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%2f53418977%2fhow-can-i-join-only-one-record-on-a-has-many-with-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 really need a separate table for
Grade
? It would a lot simpler if you just used an integer onreports
.– max
Nov 21 at 20:07
@max, yes the grade values are more complex than shown here. I just simplified it for the illustration.
– daveomcd
Nov 21 at 20:16