rails:3608
From: 前島真一 <netwillnet@g...>
Date: Tue, 16 Aug 2011 00:08:49 +0900
Subject: [rails:3608] Re: Re: Re: Rails3初心者- テーブルをまたいでfind
前島です。 結論としては橋立さんと同じなのですが書いてしまったので投げます。 まず前提として3つ。 1. :include の指定は関連名(belongs_to :maker) なので単数で :include => :maker とします 2. :conditions 中でカラム名を詳しく指定する時はテーブル名(makers)を使うので makers.maker_name のように指定します 3. join を伴うクエリを投げる場合、複数テーブルで同名のカラム名を指定する場合にはテーブル名を指定(例: products.created_at)する必要があります ログを見ると >> Product Load (0.3ms) SELECT "products".* FROM "products" WHERE (code like '%アカセ%' or name like '%アカセ%') ORDER BY created_at DESC << と書かれているので、'ORDER BY created_at DESC' を何処かで指定しているように見えます。先ほど maker.maker_name を makers.maker_name と修正したときにエラーが出たのはこの 'ORDER BY created_at DESC' にテーブル名の指定が無かったからではないかと推察します。 order を指定しているところを探して、"products.created_at DESC" のようにテーブル名を付け加えてみてください。 2011年8月15日23:35 Tomohiro Hashidate <kakyoin.hierophant@g...>: > 曽原さん > > 横から失礼します。 > 橋立と申します。 > 前島さんが書かれたコードを実行した時に出力されたエラーは、 > 発行SQLの最後の ORDER BY created_at DESCという箇所で、 > created_atがproductsテーブルか、makersテーブルのどちらのものか判断できないため、 > 発生しているエラーだと思います。 > 検索条件として正しいのは、makers.maker_nameであるはずです。 > 何故SQL文に、ORDER BY created_at DESCが付いているのかは、 > ちょっと判断できませんが、Productクラスの定義等で、 > デフォルトのORDER句等を指定していませんか? > ORDER句が定義されている箇所を、products.created_atと修正すれば、 > エラーは解消されると思います。 > > -- > Tomohiro Hashidate > Sent with Sparrow > Sparrow を使って送信 > > 日付:2011年8月15日月曜日、時刻:23:09、差出人:sohara: > > 前島様 > > 曽原です。 > > :include =>:makerだけでしたらエラーはでておりません。 > > @products =Product.find(:all,:include =>:maker, :conditions =>['code like > :value or name like :value', {:value=> '%' +params[:search_form]+ '%'}]) > > DB定義はschema,rbのデータでよろしいのでしょうか? > > どうぞよろしくお願い致します。 > > =============================== > > schema,rb > > create_table "products", :force => true do |t| > > t.string "name" > > t.integer "price" > > t.datetime "created_at" > > t.datetime "updated_at" > > t.string "code" > > t.integer "cost" > > t.integer "quantity" > > t.integer "maker_id" > > t.integer "cloth_id" > > t.integer "category_id" > > end > > create_table "makers", :force => true do |t| > > t.string "maker_name" > > t.datetime "created_at" > > t.datetime "updated_at" > > end > > =============================== > > 試した時のログです。 > > @products =Product.find(:all,:include =>:maker, :conditions =>['code like > :value or name like :value', {:value=> '%' +params[:search_form]+ '%'}]) > > Started GET > "/products/search?utf8=%E2%9C%93&search_form=%E3%82%A2%E3%82%AB%E3%82%BB&commit=%E5%95%86%E5%93%81%E6%A4%9C%E7%B4%A2" > for 127.0.0.1 at 2011-08-15 22:58:36 +0900 > > Processing by ProductsController#search as HTML > > Parameters: {"utf8"=>"", "search_form"=>"アカセ", "commit"=>"商品検索"} > > Product Load (0.3ms) SELECT "products".* FROM "products" WHERE (code like > '%アカセ%' or name like '%アカセ%') ORDER BY created_at DESC > > Rendered shared/_header.rhtml (0.9ms) > > Maker Load (0.3ms) SELECT "makers".* FROM "makers" > > Category Load (1.7ms) SELECT "categories".* FROM "categories" > > Rendered shared/_left.rhtml (96.8ms) > > Rendered shared/_footer.rhtml (0.4ms) > > Rendered products/index.html.erb within layouts/application (105.6ms) > > Completed 200 OK in 113ms (Views: 109.7ms | ActiveRecord: 3.3ms) > > ****************************************************************** > makersのコードで試した時のログです。 > @products =Product.find(:all,:include =>:maker, :conditions > =>['makers.maker_name like :value or code like :value or name like :value', > {:value=> '%' +params[:search_form]+ '%'}]) > > Started GET > "/products/search?utf8=%E2%9C%93&search_form=%E3%82%A2%E3%82%AB%E3%82%BB&commit=%E5%95%86%E5%93%81%E6%A4%9C%E7%B4%A2" > for 127.0.0.1 at 2011-08-15 23:04:57 +0900 > Processing by ProductsController#search as HTML > Parameters: {"utf8"=>"", "search_form"=>"アカセ", "commit"=>"商品検索"} > Product Load (0.3ms) SELECT "products"."id" AS t0_r0, "products"."name" > AS t0_r1, "products"."price" AS t0_r2, "products"."created_at" AS t0_r3, > "products"."updated_at" AS t0_r4, "products"."code" AS t0_r5, > "products"."cost" AS t0_r6, "products"."quantity" AS t0_r7, > "products"."maker_id" AS t0_r8, "products"."cloth_id" AS t0_r9, > "products"."category_id" AS t0_r10, "makers"."id" AS t1_r0, > "makers"."maker_name" AS t1_r1, "makers"."created_at" AS t1_r2, > "makers"."updated_at" AS t1_r3 FROM "products" LEFT OUTER JOIN "makers" ON > "makers"."id" = "products"."maker_id" WHERE (makers.maker_name like '%アカセ%' > or code like '%アカセ%' or name like '%アカセ%') ORDER BY created_at DESC > SQLite3::SQLException: ambiguous column name: created_at: SELECT > "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."price" AS > t0_r2, "products"."created_at" AS t0_r3, "products"."updated_at" AS t0_r4, > "products"."code" AS t0_r5, "products"."cost" AS t0_r6, > "products"."quantity" AS t0_r7, "products"."maker_id" AS t0_r8, > "products"."cloth_id" AS t0_r9, "products"."category_id" AS t0_r10, > "makers"."id" AS t1_r0, "makers"."maker_name" AS t1_r1, > "makers"."created_at" AS t1_r2, "makers"."updated_at" AS t1_r3 FROM > "products" LEFT OUTER JOIN "makers" ON "makers"."id" = "products"."maker_id" > WHERE (makers.maker_name like '%アカセ%' or code like '%アカセ%' or name like > '%アカセ%') ORDER BY created_at DESC > Completed in 79ms > ActiveRecord::StatementInvalid (SQLite3::SQLException: ambiguous column > name: created_at: SELECT "products"."id" AS t0_r0, "products"."name" AS > t0_r1, "products"."price" AS t0_r2, "products"."created_at" AS t0_r3, > "products"."updated_at" AS t0_r4, "products"."code" AS t0_r5, > "products"."cost" AS t0_r6, "products"."quantity" AS t0_r7, > "products"."maker_id" AS t0_r8, "products"."cloth_id" AS t0_r9, > "products"."category_id" AS t0_r10, "makers"."id" AS t1_r0, > "makers"."maker_name" AS t1_r1, "makers"."created_at" AS t1_r2, > "makers"."updated_at" AS t1_r3 FROM "products" LEFT OUTER JOIN "makers" ON > "makers"."id" = "products"."maker_id" WHERE (makers.maker_name like '%アカセ%' > or code like '%アカセ%' or name like '%アカセ%') ORDER BY created_at DESC): > app/controllers/products_controller.rb:24:in `search' > Rendered > .bundle/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb > (1.1ms) > Rendered > .bundle/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb > (4.7ms) > Rendered > .bundle/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb > within rescues/layout (9.4ms) > > > 2011年8月15日22:54 前島真一 <netwillnet@g...>: > > 前島です。 > > "maker"だとエラーは出ないですか? > あと、Product, Maker それぞれのDB定義(db/migration 配下の各ファイル) を見せてもらえますか? > > 2011年8月15日22:35 sohara <gggsjp2@g...>: >> 前島様 >> ありがとうございます。 >> maksesでやってみましたが、エラーが表示されました^^; >> どこか他にご指摘がございましたら、ご教授くださいませ。 >> @products =Product.find(:all,:include =>:maker, :conditions >> =>['makers.maker_name like :value or code like :value or name like >> :value', >> {:value=> '%' +params[:search_form]+ '%'}]) >> >> ActiveRecord::StatementInvalid in ProductsController >> >> SQLite3::SQLException: ambiguous column name: created_at: SELECT >> "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."price" >> AS >> t0_r2, "products"."created_at" AS t0_r3, "products"."updated_at" ・・・ >> >> 曽原 >> gggsjp2@g... >> >> > > > > -- > 前島 真一(@netwillnet) > http://www.willnet.in/ > http://d.hatena.ne.jp/willnet/ > > -- > ML: rails@r... > 使い方: http://QuickML.com/ > > > -- 前島 真一(@netwillnet) http://www.willnet.in/ http://d.hatena.ne.jp/willnet/
@ 3599 2011-08-15 11:12 [gggsjp2@g... ] Rails3初心者- テーブルをまたいでfind 3601 2011-08-15 15:24 ┗[netwillnet@g... ] @ 3603 2011-08-15 15:35 ┗[gggsjp2@g... ] 3605 2011-08-15 15:54 ┗[netwillnet@g... ] @ 3606 2011-08-15 16:09 ┗[gggsjp2@g... ] @ 3607 2011-08-15 16:35 ┗[kakyoin.hierophant@g] Re: Re: Rails3初心者- テーブルをまたいでfind -> 3608 2011-08-15 17:08 ┣[netwillnet@g... ] @ 3610 2011-08-15 17:40 ┃┗[gggsjp2@g... ] @ 3611 2011-08-15 18:46 ┃ ┗[kakyoin.hierophant@g] Re: Re: Re: Re: Rails3初心者- テーブルをまたいでfind @ 3612 2011-08-16 04:11 ┃ ┗[gggsjp2@g... ] 3613 2011-08-16 09:26 ┃ ┗[netwillnet@g... ] @ 3614 2011-08-17 06:58 ┃ ┗[gggsjp2@g... ] @ 3609 2011-08-15 17:21 ┗[gggsjp2@g... ]