【发布时间】:2016-06-17 20:29:10
【问题描述】:
我有一个最近更新到 Rails 5 的 Rails 应用程序。我有一个看起来像这样的数据模型(简化):Users 可以有很多 Apps,Users 也可以是 Member多个Teams,每个Team也可以有多个Apps。在我的Apps 索引视图/控制器中,我想列出他/她创建的所有用户应用程序,我还想列出属于Teams 的所有应用程序Users 是Member的。
我感觉有一种比我当前的实现更好、更高效的方法(可能是 Rails 5 中的新功能)。这是我当前实现的样子:
apps = []
# First get all the team apps where the user is a member, but haven't created the app.
current_or_guest_user.teams.each do |team|
team.apps.each do |app|
unless app.user.eql?(current_or_guest_user)
apps << app
end
end
end
# ... then get all the apps that the user have created.
current_or_guest_user.apps.each do |app|
unless apps.include?(app)
apps << app
end
end
# Return the apps.
@apps = apps
那么,有没有一种更清洁、更优化的方法来做我正在做的事情?那看起来怎么样?
编辑
这是我的活动模型关联的样子:
# App.rb
belongs_to :user
belongs_to :team
# User.rb
has_many :apps, dependent: :destroy
has_many :teams
has_many :teams, through: :members
# Team.rb
has_many :apps, dependent: :destroy
编辑 2
我想知道 Rails 5 方法 #or (https://github.com/rails/rails/pull/16052) 是否可以在这个用例中使用,例如:
current_user.apps.or([...]])
# [...] = In not exactly sure what to put here in that case.
【问题讨论】:
-
可以以格式良好的方式粘贴模型关联吗?
-
@oreoluwa 我现在已经添加了关联。
-
我认为更优化的方法是使用
Arel。我真的不认为 Rails 5 对此有更好的方法。但另一种方式是委托方法,但它可能仍然不够优化。
标签: sql ruby-on-rails ruby rails-activerecord ruby-on-rails-5