Heroku加入/包含不像sqlite开发环境那样工作 [英] Heroku join/includes not working like sqlite dev environment
问题描述
这就是我在开发环境(SQLite)中能完美运行的状态:
So this is what I have in my dev (SQLite) environment that works perfectly:
key = "%#{keywords}%"
listings = Listing.order(:headline)
listings = listings.includes(:neighborhood).where("headline like ? or neighborhoods.name like ?", key, key) if keywords.present?
但是,当我在Heroku上尝试类似的操作时,会出现很多错误:
But, when I try something similar on Heroku, I get a number of errors:
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?
我还尝试了以下&得到了相应的错误:
I also tried the following & got the corresponding error:
> 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%')
我也尝试了以下错误:
I also tried and got the following error:
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:
A列表 belo ngs_to邻居
和邻居有许多房源
。
编辑2:
提取其中一个SQL语句并通过 psql
运行它,结果是:
Extracted one of the SQL statements and ran it via psql
, this is the result:
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...
推荐答案
尝试以下操作:
listings = Listing.includes(:neighborhood).where("listings.headline like :keywords or neighborhoods.name like :keywords", :keywords => "%#{keywords}%")
我认为应该调用 listings.headline
而不是标题
I think it should call listings.headline
instead of 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之间有一些区别吗? ar 问题。我想您希望使用ILIKE而不是LIKE:
EDIT - do you know that there are some difference between SQLite and PostgreSQL? I faced a similar problem. I think you want ILIKE instead of LIKE:
listings = Listing.includes(:neighborhood).where("listings.headline ilike :keywords or neighborhoods.name ilike :keywords", :keywords => "%#{keywords}%")
这篇关于Heroku加入/包含不像sqlite开发环境那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!