-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rails] How best to name route associated with a join table that has no join model? #288
Comments
I understand you want you to try There's a few reasons we don't teach
The best practice really is to avoid
If you want to stick strictly to convention, I'd have a Better yet, if you want your path to dictate your architecture, you could go with a a namespaced Finally, I'd argue that what you really have here is a relationship between users and patterns called favorites. So User Whether you
Well, almost all routes end in plural because we try to stick to RESTful, conventional, resourceful routes.
I almost want to say that "anything is better than a nested route" but for some reason I'm still fond of them. I think we're at the point where we can manage flat routes well. Especially since Ember is your client choice, flat routes seem a win here. What isn't a win is mixing favorites and patterns. If I sent you a URL of
I hope I've convinced you this is a good move. And I think it will save a bit of headache. Thanks for giving me an excuse to wrestle with some really solid questions and I hope my answer has been helpful! |
@rhjones What @jrhorn424 said 🤖 😄 |
|
@jrhorn424 Convinced! And yeah, the noun/verb(/adjective) issue here is tricky. I blame social media (see: Facebook/Twitter "likes"). |
I'm experimenting with using
has_and_belongs_to_many
relationships between two of my resources (users and patterns; users can "favorite" a pattern) rather than ahas_many :through
. I don't need any additional attributes on the join table, which is why (based on the Rails guide to Active Record Associations) I've chosen this path.The join table that is created (using
bundle exec rails g migration CreateJoinTablePatternUser pattern user
) is calledpatterns_users
. I don't have a controller or a model for this join, as per the advice in the guide.Obtaining all of a user's favorites is possible through
user.patterns
, so I don't think I need a route there. To create a new favorite, though, I'll need some way to post to the join table directly.I'm leaning toward making a
POST
request to/patterns/:pattern_id/favorite
, and defining acreate_favorite
route inPatternsController
to add the pattern in question to the user's list of favorites.Is there a better/more semantic approach? Specific questions:
POST
generally goes to a route that ends in a plural. If that's an accurate heuristic, should my route be/patterns/:pattern_id/favorites
?favorites/:pattern_id
better than a nested route?has_and_belongs_to_many
approach and just do ahas_many :through
, with afavorite
model /favorites
controller?Not sure any of this will affect my functionality in any way, but I'm curious whether there's a best practice.
The text was updated successfully, but these errors were encountered: