gpt4 book ai didi

ruby-on-rails - 数组不包含的 Rails 5.2 Controller

转载 作者:行者123 更新时间:2023-11-29 14:26:39 25 4
gpt4 key购买 nike

我有一个名为 hardships 的模型,其中包含一个数组值,用于表示已投票批准该困难的用户(hardship_params 中的 :approvals => [] )。

我正在尝试在索引页上创建一个 hardships 列表,用于任何给定 current_user(使用设计)未批准的困难。

该数组由用户 ID 组成,因此 @hardship.approvals 的典型值将是 ["2", "3"] 如果第二个和第三个用户已经批准了它。

我在从 Controller 调用它的逻辑上遇到了问题。我看过像 this 这样的帖子但无法找到适用于 Rails 5.2 和 Postgres 的答案。

我正在寻找这样的东西:

@need_approval = Hardship.where.not(approvals.include?(current_user.id.to_s))

但是,我知道这是行不通的,因为 approvals 不是一个变量,而且语法完全错误。任何人都可以帮助我 Rails-ify 这个以便它获得我需要的信息吗?

附加信息

这是我的困难表:

  create_table "hardships", force: :cascade do |t|
t.string "full_name"
t.date "date"
t.string "position"
t.string "branch"
t.string "email_non_toca"
t.string "mobile"
t.string "address"
t.string "city"
t.string "state"
t.string "zip"
t.string "bank_name"
t.string "bank_phone"
t.string "bank_address"
t.date "start_date"
t.boolean "accident", default: false
t.boolean "catastrophe", default: false
t.boolean "counseling", default: false
t.boolean "family_emergency", default: false
t.boolean "health", default: false
t.boolean "memorial", default: false
t.boolean "other_hardship", default: false
t.string "other_hardship_description"
t.decimal "requested_amount"
t.text "hardship_description"
t.decimal "self_fund"
t.string "intent_signature"
t.date "intent_signature_date"
t.string "release_signature"
t.date "release_signature_date"
t.string "status", default: "Application Started"
t.string "final_decision", default: "Not Decided"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "returned", default: false
t.text "approvals", default: [], array: true
t.text "rejections", default: [], array: true
t.index ["user_id"], name: "index_hardships_on_user_id"
end

最佳答案

如果我是你,我想我会很想创建一个 ApplicationAction 模型。像这样的东西:

# == Schema Information
#
# Table name: application_actions
#
# id :bigint not null, primary key
# user_id :integer
# application_type :string
# application_id :integer
# action :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class ApplicationAction < ApplicationRecord
belongs_to :user
belongs_to :application, polymorphic: true

enum action: {
approved: 0,
rejected: 1,
requested_to_modify: 2
}
end

然后,在您的 User 模型中,执行如下操作:

# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class User < ApplicationRecord
has_many :application_actions

%i(
approved
rejected
requested_to_modify
).each do |action_sym|
%w(
hardship
scholarship
charity
).each do |application_type|

# Create the associations:
# - hardship_applications
# - scholarship_applications
# - charity_applications
has_many "#{application_type}_applications".to_sym,
-> { distinct },
through: :application_actions,
source: :application,
source_type: application_type.camelize

# Define the methods:
# - approved_application_actions
# - rejected_application_actions
# - requested_to_modify_application_actions
define_method("#{action_sym}_application_actions") do
application_actions.send(action_sym)
end

# Define the methods:
# - approved_hardship_applications
# - rejected_hardship_applications
# - requested_to_modify_hardship_applications
# - approved_scholarship_applications
# - rejected_scholarship_applications
# - requested_to_modify_scholarship_applications
# - approved_charity_applications
# - rejected_charity_applications
# - requested_to_modify_charity_applications
define_method("#{action_sym}_#{application_type}_applications") do
send("#{application_type}_applications").
joins(:application_actions).
where(
application_actions: {
action: ApplicationAction.actions[action_sym]
}
)
end

# Define the methods:
# - hardship_applications_not_approved
# - hardship_applications_not_rejected
# - hardship_applications_not_requested_to_modify
# - scholarship_applications_not_approved
# - scholarship_applications_not_rejected
# - scholarship_applications_not_requested_to_modify
# - charity_applications_not_approved
# - charity_applications_not_rejected
# - charity_applications_not_requested_to_modify
define_method("#{application_type}_applications_not_#{action_sym}") do
application_type.
camelize.
constantize.
where.
not(id: send("#{action_sym}_#{application_type}_applications"))
end

end
end

end

然后,在您的 Hardship 模型中,执行如下操作:

# == Schema Information
#
# Table name: hardships
#
# id :bigint not null, primary key
# application_type :integer default(NULL)
# created_at :datetime not null
# updated_at :datetime not null
#

class Hardship < ApplicationRecord
has_many :application_actions, as: :application

enum application_type: {
accident: 0,
catastrophe: 1,
counseling: 2,
family_emergency: 3,
health: 4,
memorial: 5,
other_hardship: 6
}
end

然后,如果我通过快速 RSpec 测试运行它:

require 'rails_helper'

RSpec.describe 'Hardship Applications' do
before(:each) do
@user_1 = User.create!
@user_2 = User.create!
@hardship_1 = Hardship.create!
@user_1.
application_actions.
create!(application: @hardship_1).
approved!
@user_2.
application_actions.
create!(application: @hardship_1).
rejected!
end
it "user_1 approved_hardship_applications to include hardship_1" do
expect(@user_1.approved_hardship_applications).to include(@hardship_1)
end
it "user_1 hardship_applications_not_approved NOT to include hardship_1" do
expect(@user_1.hardship_applications_not_approved).not_to include(@hardship_1)
end
it "user_1 rejected_hardship_applications NOT to include hardship_1" do
expect(@user_1.rejected_hardship_applications).not_to include(@hardship_1)
end
it "user_2 approved_hardship_applications NOT to include hardship_1" do
expect(@user_2.approved_hardship_applications).not_to include(@hardship_1)
end
it "user_2 hardship_applications_not_approved to include hardship_1" do
expect(@user_2.hardship_applications_not_approved).to include(@hardship_1)
end
it "user_2 rejected_hardship_applications to include hardship_1" do
expect(@user_2.rejected_hardship_applications).to include(@hardship_1)
end
end

我明白了...

Hardship Applications
user_1 approved_hardship_applications to include hardship_1
user_1 hardship_applications_not_approved NOT to include hardship_1
user_1 rejected_hardship_applications NOT to include hardship_1
user_2 approved_hardship_applications NOT to include hardship_1
user_2 hardship_applications_not_approved to include hardship_1
user_2 rejected_hardship_applications to include hardship_1

Finished in 0.13431 seconds (files took 0.90021 seconds to load)
6 examples, 0 failures

所以,你可以这样做:

@need_approval = current_user.hardship_applications_not_approved

代替:

@need_approval = Hardship.where.not(approvals.include?(current_user.id.to_s))

您还会注意到,在 Hardship 模型中,我将您所有的应用程序类型都放入了一个 enum 中。这将减少空字段(假设 Hardship 应用程序只能属于一种应用程序类型)。

似乎还有其他减少冗余的机会。例如,您可以创建 PhysicalAddressPhoneNumberEmailAddress 模型并将它们与每种类型的应用程序相关联。您还可以将应用程序 status 创建为 enum 并将其默认为 application_started。与 final_decision 相同。

关于ruby-on-rails - 数组不包含的 Rails 5.2 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100893/

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