I'm a bit stuck with something that I've previously done countless times in Node or Django.
I'm building an API-only Rails app.
Say I have a Post
model containing blog posts. The model has a published
attribute, which determines if a certain post is available already to the larger public.
Obviously, the goal in the controller is to return the JSON of the post(s) if the flag is true.
For collections, I have created a simple scope in my Post model as follows:
scope :published, -> { where(published: true) }
However, for single requests I am not sure what is the correct, or rather: the Rails way of handling this. I can "force it out", but I feel like there's a neat "convention over configuration" trick for things like this.
In essence, I'm curious about the cleanest solution for this: if the post's published attribute is true, return the post object, if it's false, return a 404
Should I look at scopes? Serializers? Where is the treasure hidden?
Thanks for contributing!