Reputation: 29
so I have a model :note that belongs to model :folder that takes a :folder_id. The :folder model takes a :privacy boolean that should determine whether the folder's notes can be seen by other users or not in the users#show view. So if :privacy is set to true, the folder's notes can't be seen, but they can be viewable if :privacy is set to false.
So far, I've been able to display public :folders with the following instance variable in my user_controller:
@public_folders = @user.folders.where(privacy: false)
And I've been able to get a :folder's notes when a :folder_id param is present with this:
@folder_notes = @user.notes.where(folder_id: params[:folder_id])
So my question is, how can I select all :notes that belong to any :folder where the :privacy is set to false?
Upvotes: 1
Views: 31
Reputation: 27971
@user.notes.joins(:folder).where(folders: { privacy: false })
Although, I'd make that a scope on Note
:
scope :with_public_folders, -> { joins(:folder).where(folders: { privacy: false }) }
...and then you can do:
@user.notes.with_public_folders
Upvotes: 1