我一直试图根据我的rails3应用程序中的某个范围急切加载关联,但找不到任何解决方案.
我的应用有以下型号:
class Project
has_many :entries
has_many :to_dos
class ToDo
has_may :entries
has_many :tasks
belongs_to :project
class Task
has_many :entries
belongs_to :to_do
class Entry
belongs_to :project
belongs_to :to_do
belongs_to :task
# options format: {:from_date=>(Date.today-1.week), :to_date=>(Date.today+1.week), :user_id=>60}
scope :filtered_list, lambda { |options|
condition = options[:user_id].nil? ? "true" : "user_id = #{options[:user_id]}"
condition += options[:from_date].nil? ? "" : " AND entry_date >= '#{options[:from_date]}'"
condition += options[:to_date].nil? ? "" : " AND entry_date <= '#{options[:to_date]}'"
where(condition)
}
Run Code Online (Sandbox Code Playgroud)
在项目#index中,我有以下代码来获取用户的所有项目:
@projects = current_user.projects.includes(:entries, :to_dos =>[:entries, …Run Code Online (Sandbox Code Playgroud)