I’ve set up Devise to manage the authentication to my app.
I have a Category model in which users create their own categories. User has_many :categories. This model has a user_id attribute, so when someone logs in and goes to categories/index for example, from the controller the query would bring categories using current_user.id to filter out which ones to bring.
So far straight forward and works well, nobody seems to be able to see someone else’s categories, but to be honest, unless I’m missing something, this seems a bit insecure. How do I know some hacker will not figure it out and send his own requests modifying the params?
Is this possible or am I being paranoid? Also, I might not be using the functionality properly?
I think you have the relationship setup for a one-to-one (one category per user) instead of a one-to-many (many categories per user). If you have a
category_idin theUsermodel, the following should be your setup.If you want to have multiple categories per user, than I suggest using a link table (as model
UserCategory) withuser_idandcategory_id.Then, in your
Categorycontroller you can use your code from above to grab all categories by a given user.