カスタムフィールドに基づいてトピックを取得しますか?

I would advise agianst this for multiple reasons. Sorry to be mysterious, but it’d take a bit more time than I have right now to explain why in full.

The easiest way is to use one of the existing endpoints in the list controller. They’re already setup to serve lists of topics. You can find them in routes.rb, but in short, they’re the filters /latest, /top etc. For a list with a custom filter, You’ll want to use a query param like this

/latest?fun_level=5

Using the custom filter. You can follow the TopicQuery class through from the list_controller.rb to see how it works, e.g. it adds your custom filter as a supported parameter to the controller. The reason it feels mysterious is because that controller and class are handling a bunch of things for you like pagination and different filters which you’ll need to setup manually if you do it another way.

The “other way” which would make sense (not using /search mind you), would be to set up your own dedicated controller for the route, which uses the TopicQuery class like the list_controller.rb does. You’ll need to create a rails controller if you’re adding a completely new route in any event, so this is another plausible approach, albeit you’ll need to handle things like pagination yourself. You should still use a custom filter if you use this approach.

I understand some of this may seem opaque, however you’re dealing with complicated functionality here. To explain this in full I’d need to write up a 10 part course. Which I may do soon actually :slight_smile:

「いいね!」 2