0

我在两个不同的模型中有两个字段,它们将位置值存储在一个数组中,我想要实现的是一个控制器实例变量,它可以匹配两个数组中的任何相同值,然后在索引视图中显示它。但是,当我尝试这段代码时

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

它抛出此错误:

Unsupported argument type: Hash. Construct an Arel node instead

提交模式:

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

代理模式:

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

数据如何存储在 Desired_Location 和 Company_Business_Location 字段中的示例:

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

2 回答 2

0

要在 rails 查询中匹配数组的相同值,您可以ANY of postgres在 rails中使用

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

在这里阅读更多http://www.postgresqltutorial.com/postgresql-any/

于 2019-01-10T07:52:36.710 回答
0

如果 Desired_Location 是 Submission 模型中的字段名称,则使用如下 select 方法。Select 方法不接受散列作为参数。

您可以传递一个块来检查条件并获得所需的结果。如果尽管包含所有元素但位置不同,则对数组使用排序以使数组相同。

desired_loc_arr = current_agent.Company_Business_Location
@submissions = Submission.select { |sub| sub.Desired_Location.sort == desired_loc_arr.sort }
于 2019-01-07T12:10:28.533 回答