0

Following were my query in controller:

@cref = Creference.where("lower(name) LIKE ?", "#{@city.downcase}")

  if @cref.present?
    cities_array = @cref.map {|con| "%#{con.countries}%" } 
    #cities_array return --> ["%["Kuching", "Kota Kinabalu"]%"]
    @products.where("city ILIKE ANY ( array[?] )", cities_array) --> []
end

The product doesn't return any result despite there is city with above name in Product table. Thanks!!

SCHEMA

product:

create_table "products", force: :cascade do |t|
    t.integer  "user_id"
    t.string   "name"
    t.integer  "payment_type",        limit: 2
    t.datetime "created_at",                    precision: 6,             null: false
    t.datetime "updated_at",                    precision: 6,             null: false
    t.integer  "product_category_id"
    t.integer  "location_id"
    t.text     "description"
    t.text     "highlight"
    t.integer  "price_cents"
    t.integer  "zip"
    t.string   "country"
    t.string   "state"
    t.string   "city"
    t.string   "address"
    t.string   "apt"
    t.integer  "refund_day"
    t.string   "currency"
    t.integer  "refund_percent"
    t.integer  "refundable",          limit: 2,               default: 0
    t.integer  "step",                limit: 2,               default: 0
    t.integer  "discount",                                    default: 0
    t.string   "slug"
    t.integer  "status",              limit: 2,               default: 1
    t.integer  "verification",        limit: 2,               default: 0
  end

creference:

  create_table "creferences", force: :cascade do |t|
    t.string   "name"
    t.string   "countries",  default: [],              array: true
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end
5
  • Maybe @products is empty or nil try Product.where("city ILIKE ANY ( array[?] )", cities_array) --> [] it should return products if matched any. Commented Apr 6, 2016 at 6:30
  • @MuhammadYawarAli why use Product.where? Commented Apr 6, 2016 at 6:32
  • It will search in whole table. Commented Apr 6, 2016 at 6:54
  • could you post the schema for Creference and Product model? Commented Apr 6, 2016 at 6:54
  • @Emu, check the schema updates Commented Apr 6, 2016 at 7:00

2 Answers 2

2

You can do something like that:

@cref = Creference.where("lower(name) LIKE ?", "#{@city.downcase}")

  if @cref.present?
    cities_array = @cref.collect {|con| con.countries }.flatten 
    #cities_array will return --> ["Kuching", "Kota Kinabalu"]
    @products.where("city IN (?)", cities_array) 
    # the IN query will find the city names that are in the cities_array
    # but in that case your product tables city name should be in lowercase
    # as you are fetching lowercase city names from Creference
end

N.B. I've not tested the code above. But it should work. :)

Sign up to request clarification or add additional context in comments.

Comments

0

First, check the value of cities_array to see what is returns.

Second, try to append to_sql at the end of the statement and to print it to see what the sql looks like:

puts @products.where("city ILIKE ANY ( array[?] )", cities_array).to_sql

Last but not least, Product.count to see that you actually have products in your table :).

5 Comments

This is the query i get SELECT "products".* FROM "products" INNER JOIN "users" ON "users"."id" = "products"."user_id" WHERE "products"."step" = 5 AND "products"."status" = 1 AND ("users"."merchant_status" = 1 OR "users"."merchant_status" = 0 AND "products"."verification" = 1) AND (city ILIKE ANY ( array['%["Kuching", "Kota Kinabalu"]%'] ))
Is the last part valid SQL? I don't know which DBMS you use, but it looks weird to me. You can take this SQL statement and launch it on your dbms command line, so that you can see if you have a SQL or a Rails problem.
I am using postgres. And yet, it doesnt return anything
Then go to your psql CLI and try to launch the query above.
Which is good, you have a valid SQL query. It just does not do what you want...Try to refactor your AR query to make it do what you need (maybe check Emu's answer below).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.