0

これが私の開発(SQLite)環境で完璧に機能するものです:

key = "%#{keywords}%"
listings = Listing.order(:headline)
listings = listings.includes(:neighborhood).where("headline like ? or neighborhoods.name like ?", key, key) if keywords.present?

しかし、Herokuで同様のことを試してみると、いくつかのエラーが発生します。

listings = listings.includes(:neighborhood).where("headline like ? or neighborhoods.name like ?", key, key)
ActiveRecord::ConfigurationError: Association named 'neighborhoods' was not found; perhaps you misspelled it?

また、次のことを試しましたが、対応するエラーが発生しました。

> listings = Listing.includes(:neighborhood).where("headline like :keywords or neighborhood.name like :keywords", :keywords => "%#{keywords}%")
  SQL (3.1ms)  SELECT "listings"."id" AS t0_r0, "listings"."listing_category_id" AS t0_r1, "listings"."listing_type_id" AS t0_r2, "listings"."user_id" AS t0_r3, "listings"."boro_id" AS t0_r4, "listings"."neighborhood_id" AS t0_r5, "listings"."building_id" AS t0_r6, "listings"."term_id" AS t0_r7, "listings"."headline" AS t0_r8, "listings"."property_type_id" AS t0_r9, "listings"."unit_num" AS t0_r10, "listings"."common_charges" AS t0_r11, "listings"."taxes" AS t0_r12, "listings"."maintenance" AS t0_r13, "listings"."num_bedrooms" AS t0_r14, "listings"."num_bathrooms" AS t0_r15, "listings"."square_footage" AS t0_r16, "listings"."list_square_footage" AS t0_r17, "listings"."description" AS t0_r18, "listings"."condition_id" AS t0_r19, "listings"."pet_policy_id" AS t0_r20, "listings"."exposure_id" AS t0_r21, "listings"."floor_plan" AS t0_r22, "listings"."photo" AS t0_r23, "listings"."security" AS t0_r24, "listings"."coop_deductible" AS t0_r25, "listings"."flip_tax" AS t0_r26, "listings"."house_families" AS t0_r27, "listings"."house_extensions" AS t0_r28, "listings"."house_stories" AS t0_r29, "listings"."house_units" AS t0_r30, "listings"."created_at" AS t0_r31, "listings"."updated_at" AS t0_r32, "listings"."price" AS t0_r33, "neighborhoods"."id" AS t1_r0, "neighborhoods"."name" AS t1_r1, "neighborhoods"."created_at" AS t1_r2, "neighborhoods"."updated_at" AS t1_r3, "neighborhoods"."boro_id" AS t1_r4 FROM "listings" LEFT OUTER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id" WHERE (headline like '%prospect heights%' or neighborhood.name like '%prospect heights%')
ActiveRecord::StatementInvalid: PG::Error: ERROR:  missing FROM-clause entry for table "neighborhood"
LINE 1: ..._id" WHERE (headline like '%prospect heights%' or neighborho...
                                                             ^
: SELECT "listings"."id" AS t0_r0, "listings"."listing_category_id" AS t0_r1, "listings"."listing_type_id" AS t0_r2, "listings"."user_id" AS t0_r3, "listings"."boro_id" AS t0_r4, "listings"."neighborhood_id" AS t0_r5, "listings"."building_id" AS t0_r6, "listings"."term_id" AS t0_r7, "listings"."headline" AS t0_r8, "listings"."property_type_id" AS t0_r9, "listings"."unit_num" AS t0_r10, "listings"."common_charges" AS t0_r11, "listings"."taxes" AS t0_r12, "listings"."maintenance" AS t0_r13, "listings"."num_bedrooms" AS t0_r14, "listings"."num_bathrooms" AS t0_r15, "listings"."square_footage" AS t0_r16, "listings"."list_square_footage" AS t0_r17, "listings"."description" AS t0_r18, "listings"."condition_id" AS t0_r19, "listings"."pet_policy_id" AS t0_r20, "listings"."exposure_id" AS t0_r21, "listings"."floor_plan" AS t0_r22, "listings"."photo" AS t0_r23, "listings"."security" AS t0_r24, "listings"."coop_deductible" AS t0_r25, "listings"."flip_tax" AS t0_r26, "listings"."house_families" AS t0_r27, "listings"."house_extensions" AS t0_r28, "listings"."house_stories" AS t0_r29, "listings"."house_units" AS t0_r30, "listings"."created_at" AS t0_r31, "listings"."updated_at" AS t0_r32, "listings"."price" AS t0_r33, "neighborhoods"."id" AS t1_r0, "neighborhoods"."name" AS t1_r1, "neighborhoods"."created_at" AS t1_r2, "neighborhoods"."updated_at" AS t1_r3, "neighborhoods"."boro_id" AS t1_r4 FROM "listings" LEFT OUTER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id" WHERE (headline like '%prospect heights%' or neighborhood.name like '%prospect heights%')

また、次のエラーが発生しました。

0> listings = Listing.joins(:neighborhood).where("headline like :keywords or neighborhood.name like :keywords", :keywords => "%#{keywords}%")
  Listing Load (2.7ms)  SELECT "listings".* FROM "listings" INNER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id" WHERE (headline like '%prospect heights%' or neighborhood.name like '%prospect heights%')
ActiveRecord::StatementInvalid: PG::Error: ERROR:  missing FROM-clause entry for table "neighborhood"
LINE 1: ..._id" WHERE (headline like '%prospect heights%' or neighborho...
                                                             ^
: SELECT "listings".* FROM "listings" INNER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id" WHERE (headline like '%prospect heights%' or neighborhood.name like '%prospect heights%')

考え?

ありがとう。

編集1:

リストbelongs_to neighborhood、および近隣has_many listings

編集2:

SQLステートメントの1つを抽出し、を介して実行しましpsqlた。結果は次のとおりです。

SELECT "listings".* FROM "listings" INNER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id" WHERE (headline like '%prospect heights%' or neighborhood.name like '%prospect heights%');                                                                                                                                                                 
ERROR:  missing FROM-clause entry for table "neighborhood"
LINE 1: ..._id" WHERE (headline like '%prospect heights%' or neighborho...
4

3 に答える 3

1

これを診断する最善の方法は、開発環境と本番環境が可能な限り一致していることを確認し、実際に PostgreSQL をローカルで実行することです。必然的に、SQLite と PostgreSQL の間でデバッグが困難な不整合に遭遇したり、コードをプッシュしたときに驚くことになります。

Mac を使用している場合、Heroku Postgres には、ダウンロードして構成なしで使用できるスタンドアロン アプリケーションがあります: http://postgresapp.com/

于 2013-02-11T17:56:13.937 に答える
1

以下を試してください:

listings = Listing.includes(:neighborhood).where("listings.headline like :keywords or neighborhoods.name like :keywords", :keywords => "%#{keywords}%")

listings.headline代わりに呼び出す必要があると思いますheadline

SELECT "listings".* FROM "listings" INNER JOIN "neighborhoods"   
ON "neighborhoods"."id" = "listings"."neighborhood_id"  
WHERE (
headline like '%prospect heights%'               #I mean here
or neighborhood.name like '%prospect heights%')  #also here, should be neighborhoods.name 

編集- SQLite と PostgreSQL の間にいくつかの違いがあることをご存知ですか? 同様の問題に直面しました。LIKEではなくILIKEが必要だと思います:

listings = Listing.includes(:neighborhood).where("listings.headline ilike :keywords or neighborhoods.name ilike :keywords", :keywords => "%#{keywords}%")
于 2013-02-11T14:49:49.693 に答える
1

再フォーマットされたクエリ:

SELECT "listings"."id" AS t0_r0,
       "listings"."listing_category_id" AS t0_r1,
       "listings"."listing_type_id" AS t0_r2,
       "listings"."user_id" AS t0_r3,
       "listings"."boro_id" AS t0_r4,
       "listings"."neighborhood_id" AS t0_r5,
       "listings"."building_id" AS t0_r6,
       "listings"."term_id" AS t0_r7,
       "listings"."headline" AS t0_r8,
       "listings"."property_type_id" AS t0_r9,
       "listings"."unit_num" AS t0_r10,
       "listings"."common_charges" AS t0_r11,
       "listings"."taxes" AS t0_r12,
       "listings"."maintenance" AS t0_r13,
       "listings"."num_bedrooms" AS t0_r14,
       "listings"."num_bathrooms" AS t0_r15,
       "listings"."square_footage" AS t0_r16,
       "listings"."list_square_footage" AS t0_r17,
       "listings"."description" AS t0_r18,
       "listings"."condition_id" AS t0_r19,
       "listings"."pet_policy_id" AS t0_r20,
       "listings"."exposure_id" AS t0_r21,
       "listings"."floor_plan" AS t0_r22,
       "listings"."photo" AS t0_r23,
       "listings"."security" AS t0_r24,
       "listings"."coop_deductible" AS t0_r25,
       "listings"."flip_tax" AS t0_r26,
       "listings"."house_families" AS t0_r27,
       "listings"."house_extensions" AS t0_r28,
       "listings"."house_stories" AS t0_r29,
       "listings"."house_units" AS t0_r30,
       "listings"."created_at" AS t0_r31,
       "listings"."updated_at" AS t0_r32,
       "listings"."price" AS t0_r33,
       "neighborhoods"."id" AS t1_r0,
       "neighborhoods"."name" AS t1_r1,
       "neighborhoods"."created_at" AS t1_r2,
       "neighborhoods"."updated_at" AS t1_r3,
       "neighborhoods"."boro_id" AS t1_r4
FROM "listings"
LEFT OUTER JOIN "neighborhoods" ON "neighborhoods"."id" = "listings"."neighborhood_id"
WHERE (headline LIKE '%prospect heights%'
       OR neighborhood.name LIKE '%prospect heights%');

問題は明らかです。テーブル「近隣」を含めますが、後で「近隣」を使用しようとしています。RoR でそれを修正する方法 - わかりませんが、正しい方向に向けるには十分でしょう。

于 2013-02-11T14:54:49.677 に答える