开发者

Querying through several models in Rails 3

开发者 https://www.devze.com 2023-02-13 03:46 出处:网络
I have several Models: Workspace User Asset WorkspaceUser WorkspaceAsset Workspace has many users and assets, through the two join tables (WorkspaceUser, WorkspaceAsset)

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消