0
User has_many Plans
Plan has_many PlanDates
PlanDates has an attr :ddate that is a Date
week = [Array of dates in a given week]
鑑於這種情況,我想知道如何構建該查找所有誰擁有任何具有PlanDate在給定的一週計劃的任何用戶的獨特user_ids
查詢。ActiveRecord的地方在孫子的查詢屬性
到目前爲止,我有這樣的:
week_users = User.joins(plans: :plan_dates).where.not(id: 246).where("plans.plan_dates.ddate >= ?", week.first).where("plans.plan_dates.ddate <= ?", week.last).uniq
=> PG::UndefinedTable: ERROR: invalid reference to FROM-clause entry for table "plan_dates"
LINE 1: ..." = "plans"."id" WHERE ("users"."id" != 246) AND (plans.plan...
^
HINT: There is an entry for table "plan_dates", but it cannot be referenced from this part of the query.
: SELECT DISTINCT "users".* FROM "users" INNER JOIN "plans" ON "plans"."user_id" = "users"."id" INNER JOIN "plan_dates" ON "plan_dates"."plan_id" = "plans"."id" WHERE ("users"."id" != 246) AND (plans.plan_dates.ddate >= '2015-11-02') AND (plans.plan_dates.ddate <= '2015-11-06')
的思考?
嘗試'week_users = User.joins(計劃:plan_dates).where.not(ID :246).where(「plan_dates.ddate> =?」,week.first).where(「plan_dates.ddate <=?」,week.last).uniq' – Pavan
that worked thanks!我沒有意識到你不需要在那裏的協會。如果你發佈答案我會接受 – james