I have two models with a has_many_through association, like this:
class Workspace < ActiveRecord::Base
has_many :dashboards_workspaces
has_many :dashboards, through: :dashboards_workspaces
end
class Dashboard < ActiveRecord::Base
has_many :dashboards_workspaces
has_many :workspaces, through: :dashboards_workspace
end
When I create a dashboard I have the option to select one or more workspaces and I'm able to get the dashboards with a workspace like this:
Dashboard.joins(:workspaces).all
And I'm getting the workspaces_ids this way:
foo = Dashboard.find(1)
foo.workspaces.id
But I need to only show in the view the dashboards that have the same workspace_id as the current user:
@workspaces = Workspace.where(id: current_user.workspaces)
For example:
Workspace.where(id: current_user.workspaces) = [4]
Dashboard.find(1).workspaces.id = [1, 3]
Dashboard.find(2).workspaces.id = [4]
I need to only show dashboard 2 in the view.
Is there a way that I can accomplish that using a query in my controller?
Thank you so much for your help!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…