lundi 18 mai 2015

Nested forms/models: return only records belonging to current_user

I have a music reviews site and there are 3 admins. Whenever we go to edit an album, it pulls in the nested forms for reviews and, if there are any, it shows all review records on albums#edit regardless of who the author/owner of a given record is.

How can I limit the reviews returned to only be for the current_user and still have their nested form fields built out correctly?

I've tried creating a scope but since I'm calling

@albums.reviews.build

That doesn't work. I instead made a quick method that pulls all reviews associated with an album and does a check to see if the ID of user passed to the method matches the review's user_id field, but then Rails complains about not being able to call build on an array.

I also tried:

@album.reviews.where(user_id: current_user.id).build

But nothing appears at all then, even on albums that I've reviewed.

This seems like something that should be easy to do, but I cannot, for the life of me, figure it out.

Aucun commentaire:

Enregistrer un commentaire