【发布时间】:2015-04-10 11:40:09
【问题描述】:
我有以下架构:
我希望为foreign_keys(author_id和editor_id)以及单独的keys(例如author_proposals和editor_proposals)选择调用proposals,我需要选择懒惰或急切地加载它们(例如User.includes(:proposals) 或不使用joins)。
更新:
#I have the scopes which is like this:
class User < ActiveRecord::Base
has_many :author_proposals, class_name: 'Proposal', foreign_key: :author_id
has_many :editor_proposals, class_name: 'Proposal', foreign_key: :editor_id
end
class Proposal < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: :author_id
belongs_to :editor, class_name: 'User', foreign_key: :editor_id
end
但我需要一个通用的,它会给我所有的建议(author_proposals 和editor_proposals),它也会急切地加载它们。我应该在has_many 上使用条件吗?
【问题讨论】:
-
贴出你目前为此编写的代码
-
我没有代码,这就是我要问的! :) 首选的方法是什么?它是否具有作用域并传递了一个 lambda?是用
conditions吗?
标签: ruby-on-rails ruby activerecord rails-activerecord