Fix NoMethodError error in Remixes controller#701
Open
zetter-rpf wants to merge 5 commits intomainfrom
Open
Conversation
This might happen when loading a project that no users have started yet. It seems a waste to call the API in this case
8852dd2 to
2a622ba
Compare
Test coverage89.93% line coverage reported by SimpleCov. |
2a622ba to
9130a84
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a production exception when listing students for a project/remix set where the school could previously be inferred as nil. It does so by passing the school explicitly, tightening error handling, and updating tests to reflect the intended behavior.
Changes:
- Rename Project “users” helpers to “students” and pass the school explicitly when looking up student profiles.
- Skip Profile API lookups when there are no student IDs to fetch, and tighten rescue behavior in
SchoolStudent::List. - Update request/model/unit specs to cover the new behavior and prevent silent failures.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/requests/projects/remix_spec.rb | Updates remix request spec to stub Profile API and assert student name is returned. |
| spec/models/project_spec.rb | Renames/rewires tests for .students, .with_students, #with_student behavior. |
| spec/concepts/school_student/list_spec.rb | Adds coverage for empty student_ids and narrows failure case to Faraday errors. |
| lib/concepts/school_student/list.rb | Skips API call when student_ids empty; rescues only Faraday::Error. |
| app/views/api/projects/remixes/index.json.jbuilder | Switches to @projects_with_students collection for rendering. |
| app/models/project.rb | Replaces .users / .with_users / #with_user with student equivalents. |
| app/controllers/api/projects_controller.rb | Uses with_student to populate @user on project show. |
| app/controllers/api/projects/remixes_controller.rb | Uses with_students(project.school, current_user) to attach student data to remixes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
I don't like this pattern of rescuing StandardError - it means that errors in development and test are hidden. Also, since we're reporting all the errors to sentry it creates a lot of noise. In this case, it made an error that happened when listing students on a project that had none. This error happened as soon as a teacher creates a project and even in many tests but because we were rescuing it we didn't notice it. I will fix this problem in the next commit. More generally, I think we should only handle expected errors - for example, a network error is an expected error. We shouldn't try to deal with unexpected errors as we can't predict what they are or how to handle them. I've looked at sentry and the only other errors logged by this method in the last month are ones from Faraday - `BadRequestError`, `ForbiddenError`, `ServerError` and `UnauthorizedError`. I'd like to remove this rescue too eventually, but I would want to understand the Faraday errors first. I've also included `ProfileApiClient::Error` as I saw `ProfileApiClient::Student422Error` errors being raised in school students controller. The remix spec was failing after this change as it was swallowing an error before. Improve the test coverage and fix the failure. Fixup error
The .users and .with_users methods are intended to be called on associations, and infer the school by looking for the school in one of the projects in the association. This can lead to errors (when there are no projects with a school) or unexpected behaviour if trying to call on a scope containing different projects. By requiring the school to be passed it, it makes clear to the caller that a school is required and it only works for a single school As part of this I have fixed the related tests - some of these were passing in unexpected ways because an error was being rescued (see previous commit).
school is a method available as an association so this isn't needed
When working with this code I found it confusing as specifically returns students, not teachers that may created projects. I haven't changed the user variable in the projects controller as I think that might be other types of user.
9130a84 to
5e5e133
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
SchoolStudent::List#list_students#548What's changed?
The cause of the issue is when list students is called with no students. Previously the school was inferred by by the students. Now the school is explicitly passed in.
As part of this I've also:
rescuein list students more specificSee commits for more