ruby on rails: how does this sql query work exactly?
What does the @posts line do exactly?
def index
if params[:user_id] && params[:artist_id]
@id = params[:user_id]
@name = Artist.find(params[:artist_id]).name
@posts = Post.includes(:user).where('users.id' => @id).joins(:artists).where('artists.name' => @name)
end
end
It seems to give me this really long query: confused as to why it needs all this.
Thanks
Started GET "/users/example4/artists/22/posts" for 127.0.0.1 at 2011-07-29 16:34:48 -0700
Processing by PostsController#index as HTML
Parameters: {"user_id"=>"example4", "artist_id"=>"22"}
Artist Load (0.2ms) SELECT "artists".* FROM "artists" WHERE "artists"."id" = 22 LIMIT 1
Post Load (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "posts"."content" AS t0_r2, "posts"."user_id" AS t0_r3, "posts"."created_at" AS t0_r4, "posts"."updated_at" AS t0_r5, "posts"."item_name" AS t0_r6, "posts"."a_name" AS t0_r7, "posts"."image" AS t0_r8,
"posts"."collection_id" AS t0_r09, "posts"."featured_post" AS t0_r10, "users"."id" AS t1_r0,
"users"."email" AS t1_r1, "users"."encrypted_password" AS t1_r2, "users"."reset_password_token" AS
t1_r3, "users"."remember_token" AS t1_r4, "users"."remember_created_at" AS t1_r5,
"users"."sign_in_count" AS t1_r6, "users"."current_sign_in_at" AS t1_r7, "users"."last_sign_in_at"
AS t1_r8, "users"."current_sign_in_ip" AS t1_r9, "users"."last_sign_in_ip" AS t1_r10,
"users"."created_at" AS t1_r11, "users"."updated_at" AS t1_r12, "users"."name" AS t1_r13,
"users"."username" AS t1_r14, "users"."bio" AS t1_r15, "users"."avatar" AS t1_r16,
"users"."cached_slug" AS t1_r17, "users"."bg_image" AS t1_r18, "users"."bg_tile" AS t1_r19 FROM
"posts" INNER JOIN "artisanships" ON "posts"."id" = "开发者_C百科artisanships"."post_id" INNER JOIN "artists"
ON "artists"."id" = "artisanships"."artist_id" LEFT OUTER JOIN "users" ON "users"."id" =
"posts"."user_id" WHERE "users"."id" = 0 AND "artists"."name" = 'bobby' ORDER BY posts.created_at
DESC
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
Rendered posts/artists.html.erb within layouts/application (275.3ms)
Completed 200 OK in 481ms (Views: 316.4ms | ActiveRecord: 3.4ms)
I'd recommend reading the AR querying guide if you haven't yet.
The code you've pasted is from a controller–and in that action, you're asking for multiple things. That's why multiple queries are taking place.
This line:
@name = Artist.find(params[:artist_id]).name
is querying the artists table and getting the name attribute from the result
While this line:
@posts = Post.includes(:user).where('users.id' => @id).joins(:artists).where('artists.name' => @name)
has both an .includes and a .joins in it ... that's why all the querying is required. As long as you have proper indexes created in your table, it shouldn't be a problem.
It's using a method called eager loading. That's what the includes does.
What this means is instead of when you do
@posts.first.user
rails querying again it writes 3 queries and calls all the users for the post and loads them into memory. That way you cut down on query counts when you do something like
@posts.each do |post|
pusts post.user
The joins is the same as an SQL join
精彩评论