这个日志中的sql语句是什么意思?

时间:2011-10-14 00:38:11

标签: sql ruby-on-rails ruby-on-rails-3 left-join outer-join

由于某种原因,即使数据库中有数据,视图也是空白的。在日志中显示sql语句选择表但我真的不明白,有人可以帮助我,这里发生了什么?提前感谢< / p>

控制器

  @city = City.find(session[:city_id])
  @store_deals=StoreDeal.where("stores.city_id = ?", session[:city_id]).includes(:deal,  :store => [:city])

视图

   <% @store_deals.each do |store_deal| %>
   <%= store_deal.deal.item_name %>
   <%= store_deal.store.store_name %>
   <%end%>

在日志中

      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)

模型

  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

1 个答案:

答案 0 :(得分:1)

这是与您的@store_deals = StoreDeal.where ...行对应的查询。当您使用includes时,Rails使用列别名(例如AS t1_r5)来跟踪结果中哪些列属于哪个对象从结果创建对象时。

您的视图可能是空白的,因为@store_deals为空,即查询未返回任何行。您是否尝试过检查它是否包含您期望的内容?