我想要得到這樣的結果: 被公佈,有一個特定的標記,並且包括標籤的所有帖子(避免N + 1)預先加載的Rails 3的ActiveRecord模型的基表條件
這裏是我的代碼:
@posts = Post.includes(
:tags
).where(
:status => 'published', :tags => { :name => params[:tag_name] }
).order(
'published_at DESC'
)
這裏是軌道的輸出:
Parameters: {"tag_name"=>"family"} Post Load (1.1ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "posts"."body" AS t0_r2, "posts"."published_at" AS t0_r3, "posts"."excerpt" AS t0_r4, "posts"."slug" AS t0_r5, "posts"."created_at" AS t0_r6, "posts"."updated_at" AS t0_r7, "posts"."status" AS t0_r8, "tags"."id" AS t1_r0, "tags"."name" AS t1_r1, "tags"."created_at" AS t1_r2, "tags"."updated_at" AS t1_r3 FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id" WHERE "posts"."status" = 'published' AND "tags"."name" = 'family' ORDER BY published_at DESC Completed in 102ms
以下是錯誤消息:
/home/sam/.rvm/gems/[email protected]/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:44:in `eval': missing attribute: status
我可以從該posts.status
列化名爲t0_r8
的SQL看,但我怎麼能得到正常的尊重我的條件?
編輯 查詢我有,這不工作:
@posts = Post.joins(
:tags
).where(
"posts.status = ? AND tags.name = ?",
"published",
params[:tag_name]
).order(
"published_at DESC"
)