Querying through several models in Rails 3
I have several Models:
Workspace
User
Asset
WorkspaceUser
WorkspaceAsset
Workspace has many users and assets, through the two join tables (WorkspaceUser, WorkspaceAsset)
I'm trying to find the most efficient and elegant way to find out if there exists a path between a User and an Asset, ie User -> WorkspaceUser -> Workspace -> WorkspaceAsset -> Asset
This is what I have开发者_如何学Python so far:
Workspace.joins(:workspace_assets, :workspace_users).where("workspace_assets.asset_id = ? & workspace_users.user_id = ?", assetID, userID)
Was hoping for a better solution and perhaps one that would return the asset in question.
Have you tried:
Asset.joins(:workspace_assets => {:workspace => :workspace_users}.
where("assets.id = ? & workspace_users.user_id = ?", assetID, userID)
精彩评论