ruby on rails - ActiveRecord where query on grandchildren's attributes -
user has_many plans plan has_many plandates plandates has attr :ddate date week = [array of dates in given week]
given that, i'd constructing query finds unique user_ids
of users have plan has plandate in given week.
so far have this:
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 from-clause entry table "plan_dates" line 1: ..." = "plans"."id" ("users"."id" != 246) , (plans.plan... ^ hint: there entry table "plan_dates", cannot referenced part of query. : select distinct "users".* "users" inner join "plans" on "plans"."user_id" = "users"."id" inner join "plan_dates" on "plan_dates"."plan_id" = "plans"."id" ("users"."id" != 246) , (plans.plan_dates.ddate >= '2015-11-02') , (plans.plan_dates.ddate <= '2015-11-06')
thoughts?
pg::undefinedtable: error: invalid reference from-clause entry table "plan_dates"
the below should work
week_users = user.joins(plans: :plan_dates).where.not(id: 246).where("plan_dates.ddate >= ?", week.first).where("plan_dates.ddate <= ?", week.last).uniq
you can simplify below
week_users = user.joins(plans: :plan_dates).where.not(id: 246).where(plan_dates: { ddate: week.first..week.last }).uniq
Comments
Post a Comment