在rails中嵌套搜索

时间:2014-06-17 12:05:34

标签: ruby-on-rails ruby postgresql ruby-on-rails-4 nested

我有一个current_user是一个顾问,有很多项目

 has_many :advisorizations, :dependent => :destroy
 has_many :projects, :through => :advisorizations

每个项目都有很多用户;

 has_many :projectizations, :dependent => :destroy
 has_many :users, :through => :projectizations

现在我想在curren_users用户中搜索,即。作为current_user是顾问的项目的用户的用户。

current_user.projects.users

给出了一个错误,比如“未定义的方法`用户'#

2 个答案:

答案 0 :(得分:3)

您应该能够通过添加到您的顾问类来实现这一目标:

has_many :advisorizations, :dependent => :destroy
has_many :projects, :through => :advisorizations
has_many :projectizations, :through => :projects
has_many :users, :through => :projectizations

然后你应该可以做current_user.users虽然这可能会令人困惑

答案 1 :(得分:0)

 @users = []   
 current_user.projects.each{|project| @users << project.users }