gpt4 book ai didi

ruby-on-rails - Ruby on rails TypeError(无法将 Hash 转换为整数)

转载 作者:行者123 更新时间:2023-12-04 06:29:13 25 4
gpt4 key购买 nike

我在 Controller 中有一个类作为客户端 API,它在不检查 organization_id 的情况下工作,organization_id 通过用户和成员链接到观察。

 def update
@observation = Observation.find(params[:id])
if params[:state] == 'submitted' && @observation.submit
if @observation.source.source_type == 'registered'
@user = User.find(id: @observation.source.source_identifier)
@memberships = Membership.find(user_id: @user.id)
if @memberships.present? &&@memberships.where("organization_id IS NOT NULL")
@observation.approve
end
end

render json: { createdAt: @observation.created_at }, status: 200
else
render nothing: true, status: 406
end
end

TypeError(无法将 Hash 转换为整数): app/controllers/api/client/v1/observations_controller.rb:106:in `update'这是行:

@user = User.find(id: @observation.source.source_identifier)

表结构:

  # create_table "sources", force: true do |t|
# t.string "device_id"
# t.string "source_type"
# t.string "source_identifier"
# ...

# create_table "users", force: true do |t|
# # t.string "email", default: "", null: false
# t.string "encrypted_password", default: "", null: false
# ...
# t.string "first_name"
# t.string "last_name"
# t.string "phone"
# t.integer "source_id"
# ...

# create_table "memberships", force: true do |t|
# t.integer "user_id"
# t.integer "organization_id"
# t.datetime "verified_at"
# ...

最佳答案

假设您正在运行 Rails 4+,要按属性查找记录,只需使用此模式:

Model.find_by(attribute_name: attribute_value)

因此,在您的情况下:

@user = User.find_by(source_identifier: @observation.source.source_identifier)

关于ruby-on-rails - Ruby on rails TypeError(无法将 Hash 转换为整数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com