Skip to content

Fix NoMethodError error in Remixes controller#701

Open
zetter-rpf wants to merge 5 commits intomainfrom
fix-nil-school-id-bug
Open

Fix NoMethodError error in Remixes controller#701
zetter-rpf wants to merge 5 commits intomainfrom
fix-nil-school-id-bug

Conversation

@zetter-rpf
Copy link
Contributor

@zetter-rpf zetter-rpf commented Feb 27, 2026

Status

What'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:

  • Changed some of the wording from users -> students when the methods are only expected to work with students not users in general
  • Skipped calling the API when there are no students to get details for (i.e. when a project has not been remixed by any students yet)
  • Made the rescue in list students more specific
  • Fixed tests that were previously incorrectly passing because of an error that was being raised and silently captured.

See commits for more

@cla-bot cla-bot bot added the cla-signed label Feb 27, 2026
This might happen when loading a project that no users have started yet. It seems a waste to call the API in this case
@zetter-rpf zetter-rpf force-pushed the fix-nil-school-id-bug branch from 8852dd2 to 2a622ba Compare March 5, 2026 16:32
@github-actions
Copy link

github-actions bot commented Mar 6, 2026

Test coverage

89.93% line coverage reported by SimpleCov.
Run: http://github.com/RaspberryPiFoundation/editor-api/actions/runs/22758151607

@zetter-rpf zetter-rpf force-pushed the fix-nil-school-id-bug branch from 2a622ba to 9130a84 Compare March 6, 2026 09:25
@zetter-rpf zetter-rpf marked this pull request as ready for review March 6, 2026 09:30
Copilot AI review requested due to automatic review settings March 6, 2026 09:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@zetter-rpf zetter-rpf force-pushed the fix-nil-school-id-bug branch from 9130a84 to 5e5e133 Compare March 6, 2026 09:50
@zetter-rpf zetter-rpf changed the title Fix nil school id bug Fix NoMethodError error in Remixes controller Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoMethodError exception in SchoolStudent::List#list_students

2 participants