2016-03-22 10 views
1

grand-parent、parent、childの3つのモデル:Organization,CategoryPostがあります。コレクションとgrand-parentの両方で.whereを使用してスコープを作成

私は、私のPostモデルにスコープを作成しようとして渡されたコレクションの最初のwhereを使用して、壮大な親にしています:

scope :ready, -> { 
    where("next_setup_at < ?", DateTime.current) 
    .joins(category: :organization) 
    .where("organizations.active = ?", true) 
} 

しかし、Postgresは私にエラー投げている:

ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR: column reference "next_setup_at" is ambiguous LINE 1: ...zations"."id" = "categories"."organization_id" WHERE (next_setup... ^

: SELECT "posts".* FROM "posts" INNER JOIN "categories" ON "categories"."id" = "posts"."category_id" INNER JOIN "organizations" ON "organizations"."id" = "categories"."organization_id" WHERE (next_setup_at < '2016-03-22 15:57:19.971887') AND (organizations.active = 't')

答えて

2

.where句をご覧ください。 2番目の方法は、照会する列を定義するのに適しています。

where("organizations.active = ?", true) 

最初のものはありません。

where("next_setup_at < ?", DateTime.current) 

next_setup_at列が参照する表を定義する必要があります。さらにimporvementは

where("posts.next_setup_at < ?", DateTime.current) 

をリードするあなたは簡単そうのような純粋なActiveRecordの中で参照するためにどのようなテーブルを指定することができます

where(posts: {next_setup_at: DateTime.current}, categories: {active: true}) 
+0

'WHERE'方法は、関係者、テーブル名を使用していません名。 – MrYoshiji

+0

ありがとう!素敵なつか:) –

関連する問題