What does this sql statement in the log means?
For some reason the view is blank even though there is data in the database.In the log shows sql statement selecting the tables but i dont really understand it,Can anybody help me ,what is going on here?Thanks in advance
controller
@city = City.find(session[:city_id])
@store_deals=StoreDeal.where("stores.city_id = ?", session[:city_id]).includes(:deal, :store => [:city])
view
<% @store_deals.each do |store_deal| %>
<%= store_deal.deal.item_name %>
<%= store_deal.store.store_name %>
<%end%>
In the log
SQL (0.3ms) SELECT "store_deals"."id" AS t0_r0, "store_deals"."store_id" AS t0_r1,
"store_deals"."deal_id" AS t0_r2, "store_deals"."created_at" AS t0_r3, "store_deals".
"updated_at" AS t0_r4, "store_deals"."store_name" AS t0_r5,
"store_deals". "address" AS t0_r6, "deals"."id" AS t1_r0, "deals"."size" AS t1_r1, "deals"."item_name" AS t1_r2,
"deals"."brand" AS t1_r3, "deals"."stars" AS t1_r4, "deals"."created_at" AS t1_r5, "deals"."updated_at" AS t1_r6, "deals"."city_id" AS t1_r7,
"deals"."category_id" AS t1_r8, "deals"."price" AS t1_r9, "stores"."id" AS t2_r0, "stores"."store_name" AS t2_r1, "stores"."created_at" AS t2_r2, "stores".
"updated_at" AS t2_r3, "stores"."address" AS t2_r4, "开发者_高级运维stores"."city_id" AS t2_r5, "cities"."id" AS t3_r0, "cities"."name" AS t3_r1, "cities".
"created_at" AS t3_r2, "cities"."updated_at" AS t3_r3 FROM "store_deals" LEFT OUTER JOIN "deals" ON "deals"."id" = "store_deals"."deal_id" LEFT
OUTER JOIN "stores" ON "stores"."id" = "store_deals"."store_id" LEFT OUTER JOIN "cities" ON "cities"."id" = "stores"."city_id" WHERE (stores.city_id = 2)
Models
class City < ActiveRecord::Base
has_many :stores
has_many :deals
end
class Deal < ActiveRecord::Base
belongs_to :city
has_many :stores ,:through =>:store_deals
has_many :store_deals
end
class StoreDeal < ActiveRecord::Base
belongs_to :store
belongs_to :deal
end
That is the query that corresponds to your @store_deals = StoreDeal.where ...
line. When you use includes
Rails uses column aliases (e.g. AS t1_r5
) to keep track of which columns in the results belong to which object when it creates objects from the results.
It's likely your view is blank because @store_deals
is empty, i.e. the query returned no rows. Have you tried inspecting it to see if it contains what you expect it to?
精彩评论