急切加载Rails 3 ActiveRecord模型,基表上有条件

时间:2011-04-29 00:57:22

标签: ruby-on-rails ruby-on-rails-3

我正在尝试获得此结果: 发布的所有帖子都有一个特定的标签,并包含标签(以避免n + 1)

这是我的代码:

@posts = Post.includes(
  :tags
).where(
  :status => 'published', :tags => { :name => params[:tag_name] }
).order(
  'published_at DESC'
)

这是rails的输出:

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/ruby-1.8.7-p334@global/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:44:in `eval': missing attribute: status

我可以从SQL中看到posts.status列的别名是t0_r8,但我怎样才能让它正确地尊重我的情况呢?

修改 查询我有,这确实有效:

@posts = Post.joins(
  :tags
).where(
  "posts.status = ? AND tags.name = ?",
    "published",
    params[:tag_name]
).order(
  "published_at DESC"
)

1 个答案:

答案 0 :(得分:0)

嗯,也许我只是没有完全理解ActiveRecord的工作方式;这是我提出的解决方案:

@posts = Post.includes(
  :tags
).joins(
  :tags
).where(
  "posts.status = ? AND tags.name LIKE ?",
  "published",
  params[:tag_name]
).order(
  "published_at DESC"
).paginate(
  :per_page => 5,
  :page => params[:page],
  :order => "published_at DESC"
)

我的错误在于假设joinsincludes会复制功能,显然不是:

Post Load (1.7ms) SELECT "posts".* FROM "posts" INNER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" INNER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id" WHERE (posts.status = 'published' AND tags.name LIKE 'family') ORDER BY published_at DESC Tag Load (1.1ms) SELECT "tags".*, t0.post_id as the_parent_record_id FROM "tags" INNER JOIN "posts_tags" t0 ON "tags".id = t0.tag_id WHERE (t0.post_id IN (7,6,4))