我需要帮助找出在rails中查询的最佳方法

时间:2022-02-13 02:05:20

I'm having a problem coming up with a query that would work for these models. I have three models that are connected.

我遇到了一个问题,提出了适用于这些模型的查询。我有三个连接的模型。

An Organization has many Users, and Users have many StatusEntries

组织有许多用户,用户有许多StatusEntries

Basically this means I could do

基本上这意味着我可以做到

Organization.find(1).users.find(1).status_entries

And have a list of status_entries returned to me.

并有一个status_entries列表返回给我。

The problem is I am trying to find a list of status_entries for a specific Organization. I am having trouble coming up with a way of doing this that is not overly complicated and is elegant. Any help would be much appreciated.

问题是我正在尝试查找特定组织的status_entries列表。我无法想出一种不太复杂且优雅的方法。任何帮助将非常感激。

1 个解决方案

#1


I think it's a case for a have_many :through association :

我认为这是一个关于has_many的案例:通过关联:

class Organization
  has_many :users
  has_many :status_entries, :through => :users
end

then you can do :

然后你可以这样做:

Organization.find(1).status_entries

#1


I think it's a case for a have_many :through association :

我认为这是一个关于has_many的案例:通过关联:

class Organization
  has_many :users
  has_many :status_entries, :through => :users
end

then you can do :

然后你可以这样做:

Organization.find(1).status_entries