0

I have two fields in two different models which store location values in an array and what I am trying to achieve is a controller instance variable that can match any identical values in both arrays and then show that in the index view. However, when I try this code

@submissions = Submission.select(Desired_Location: current_agent.Company_Business_Location)

its throws this error:

Unsupported argument type: Hash. Construct an Arel node instead

Submission Schema:

create_table "submissions", force: :cascade do |t|
t.string "First_Name"
t.string "Last_Name"
t.integer "Phone"
t.string "Email"
t.string "Desired_Location"
t.integer "number_of_beds"
t.integer "number_of_occupants"
t.integer "Rent_price_per_month_gbp"
t.date "Max_move_in_date"
t.string "Tenant_Occupation"
t.string "Contact_me_on"
t.boolean "Furnished"
t.string "Current_Address"
t.text "Property_Requirements"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "location_id"
t.index ["location_id"], name: "index_submissions_on_location_id"
t.index ["user_id"], name: "index_submissions_on_user_id"
 end

Agent Schema:

create_table "agents", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "Company_Name"
t.string "Company_Email"
t.string "Company_Phone"
t.string "Company_Address"
t.string "Company_Business_Location"
t.string "Contact_Name"
t.string "Contact_Email"
t.string "Contact_Phone"
t.integer "location_id"
t.index ["email"], name: "index_agents_on_email", unique: true
t.index ["location_id"], name: "index_agents_on_location_id"
t.index ["reset_password_token"], name: 
"index_agents_on_reset_password_token", unique: true
 end

Sample of how data is stored in both Desired_Location and Company_Business_Location fields:

["", "Abbey Wood", "Acton", "Anerley", "Angel"]

2 Answers2

0

If Desired_Location is the field name in the Submission model then use the select method as below. Select method won't accept a hash as an argument.

You can pass a block to check the condition and get the desired result. Used sort on the array to make the array identical if it's positioned differently in spite of containing all the elements.

desired_loc_arr = current_agent.Company_Business_Location
@submissions = Submission.select { |sub| sub.Desired_Location.sort == desired_loc_arr.sort }
Dyaniyal Wilson
  • 1,012
  • 10
  • 14
0

To match with identical values of an array in rails query, you can use ANY of postgres in rails

Submission.where(? = ANY(Desired_Location)", current_agent.Compay_Business_Location)

Read more here http://www.postgresqltutorial.com/postgresql-any/

abby37
  • 597
  • 6
  • 21