rails3 앱의 일부 범위를 기반으로 열심히 부하 연결을 시도했지만 해결책을 찾지 못했습니다. 따라서,rails3 범위 지정으로 Eagerloading
@projects = current_user.projects.includes(:entries, :to_dos =>[:entries, :tasks => :entries])
그것은 사용자의 모든 프로젝트를 가져옵니다 : 내가 코드를 다음 한
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)
}
그리고 프로젝트 # 지수는 사용자의 모든 프로젝트를 얻을 :
내 응용 프로그램은 다음과 같은 모델을 가지고 열정적으로 협회를 로딩했다. 그래서 다음 루프를 수행하여 프로젝트 내의 모든 항목을 가져 오면 새 쿼리가 시작되지 않습니다.
이 열렬한로드가 모든 항목을 가져 오므로, 실제로 필요한 것보다 너무 많은 데이터입니다. 그래서로드 된 항목에 몇 가지 조건을 적용하려고했지만 어떤 해결책도 찾을 수 없었습니다. 나는 다음과 같은 것을 찾고 있었다 :
@projects = current_user.projects.includes(:entries.filtered_list(options), :to_dos =>[:entries.filtered_list(options), :tasks => :entries.filtered_list(options)])
일부 조건을 만족하는 항목 만로드되도록.
eager loading으로 스코핑을 사용할 수 없습니까? 범위 지정과 함께 eagerloading을 사용하도록 도와주세요.