我想is_deleted: true
通过以下行将记录中的一列更改为:
UserTag.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true)
Run Code Online (Sandbox Code Playgroud)
但我收到了 Rubocop 的错误:
Rails/skipsmodelvalidations: avoid using update_all because it skips validations.
我知道我可以使用每个块并通过块中的 e.update 更新所有列,但update_all
速度更快,一行,简洁的解决方案。
如何仅针对该特定行禁用此类 rubocop 错误?
我正在尝试通过 Postman 模拟对我的 Rails 应用程序的 webhook POST 请求(在真实的工作流程中运行良好)。我找到了很多例子,但没有一个起作用 - 我不断收到 401 代码。我所做的是定义标题,Pre-request Script
如下所示:
JSPre-request Script
基于此文档
postman.setEnvironmentVariable("hmac", CryptoJS.HmacSHA256(request.data, \'my_secret_string\').toString(CryptoJS.digest));\n
Run Code Online (Sandbox Code Playgroud)\n但我仍然收到 401 错误。
\n我用来触发 webhook 的外部 API 文档明确指出:
\n\n\n每个 Webhook 将使用 \xe2\x80\x8b X-AQID-Signature\xe2\x80\x8b 标头发送,该标头是通过使用 HMAC 方法和 SHA256 算法对请求的有效负载进行哈希处理而创建的,使用共享密钥作为盐。这意味着在收到有效负载后,您可以通过复制哈希方法来验证其完整性。
\n
正如我所说,它在现实生活工作流程中运行良好,因此我在邮递员实现中遇到了错误。我错过了什么?
\njavascript ruby-on-rails hmac postman postman-pre-request-script
我有两个具有可选关系的模型has_many
- belongs_to
,如下所示:
class Journey < ApplicationRecord
has_many :activities, dependent: :destroy, foreign_key: 'cms_journey_id'
end
class Activity < ApplicationRecord
belongs_to :journey, optional: true, foreign_key: 'cms_journey_id'
end
Run Code Online (Sandbox Code Playgroud)
如您所见,关系基于非标准foregin_key
名称(两个模型通过名为 的记录相互链接cms_journey_id
)。添加后,foreign_key: 'cms_journey_id'
我在两个模型中都遇到了 Rubocop 错误:
Rails/InverseOf: Specify an `:inverse_of` option
Run Code Online (Sandbox Code Playgroud) 我想将服务对象添加到我的控制器中。是否有机会在此服务对象中包含 Flash 消息?
user_stocks_controller
class UserStocksController < ApplicationController
def create
@user_stock = UserStocksCreator.new(current_user, params).call
redirect_to my_portfolio_path
end
end
Run Code Online (Sandbox Code Playgroud)
服务对象user_stocks_creator
class UserStocksCreator
def initialize(current_user, params)
@current_user = current_user
@params = params[:stock_ticker]
end
def call
stock = Stock.find_by_ticker(params)
if stock.blank?
stock = Stock.new_from_lookup(params)
stock.save
end
@user_stock = UserStock.create(user: current_user, stock: stock)
flash[:success] = "Stock #{@user_stock.stock.name} was successfully added to portfolio"
end
private
attr_accessor :current_user, :params
end
Run Code Online (Sandbox Code Playgroud)
使用此代码,我有一个错误:
未定义的局部变量或方法`flash'
我想更改先前定义的属性let
以使我的测试有效。我知道它是哈希中的哈希,但我的解决方案都不起作用。
我有几个类似的let
但看起来像这样
let(:second_data) do
{
'id' => second.id.to_s,
'type' => 'account',
'attributes' =>
{
'status' => 'new',
'created_at' => second.created_at.as_json,
'time_information' => second.credit.process.date_of_interest.as_json
}
}
end
Run Code Online (Sandbox Code Playgroud)
最后这些let被合并为一个
let(:json_serialized_offers) do
{
'data' => [first_data, second_data, third_data],
'included' => first_included + second_included + third_included
}
end
Run Code Online (Sandbox Code Playgroud)
现在我想将状态更改为expired
in second_data
which is 嵌套在data
in部分:json_serialized_offers
(如上所示)。
我试图在正确的背景下再次澄清它,通过
context "when status 'closed' passed " do
let(:json_serialized_offers) do
{
'data' => second_data { status: 'expire' }
}
end
# …
Run Code Online (Sandbox Code Playgroud) 在我的 Rails 6 应用程序中,我有定义active
范围的模型 Journey:
class Journey < ApplicationRecord
has_many :users, through: :journey_progresses
has_many :journey_progresses, dependent: :destroy
scope :active, -> { where(is_deleted: false) }
end
Run Code Online (Sandbox Code Playgroud)
在我的端点中,我想显示用户旅程进度以及序列化器旅程本身。为此,我使用下面的查询,效果很好
def query
current_user.journey_progresses.includes(:journey)
end
Run Code Online (Sandbox Code Playgroud)
如何修改上述查询以仅显示活动旅程?我试图添加类似的位置current_user.journey_progresses.includes(:journey).where(is_deleted: false)
,但出现错误:
Caused by PG::UndefinedColumn: ERROR: column journey_progresses.is_deleted does not exist
Run Code Online (Sandbox Code Playgroud)
根据我尝试过的这个答案current_user.journey_progresses.includes(:journey).where(journey: {is_deleted: false} )
,但我收到另一个错误:
Caused by PG::UndefinedTable: ERROR: missing FROM-clause entry for table "journey"
Run Code Online (Sandbox Code Playgroud)
此类操作的正确语法是什么?
在我的 Rails 6/Grape API 应用程序中,我有一个模型SurveyResult
,用于存储用户提交的调查结果。
给定问题的调查结果可能很少(它可以是多项选择题):
[31] pry(main)> SurveyResult.where(survey: survey, user: user)
#<SurveyResult:0x00007f9283adca10
id: 18,
user_id: 1,
survey_id: 1,
cms_question_id: 5844869,
cms_answer_id: 321,
#<SurveyResult:0x00007f9283adc7e0
id: 20,
user_id: 1,
survey_id: 1,
cms_question_id: 5899779,
cms_answer_id: 0987,
#<SurveyResult:0x00007f9283adc920
id: 21,
user_id: 1,
survey_id: 1,
cms_question_id: 5899779,
cms_answer_id: 12,
Run Code Online (Sandbox Code Playgroud)
例如,相同的结果有两个cms_question_id
- 5899779
(如上)。现在,如果我想计算所有用户,SurveyResults
我将得到以下结果:
[31] pry(main)> SurveyResult.where(survey: survey, user: user).count
=> 3
Run Code Online (Sandbox Code Playgroud)
如何修改这个查询计数SurveyResult
用相同的cms_question_id
只有一次?上面例子的预期结果应该是:
[31] pry(main)> SurveyResult.where(survey: survey, user: user).count
=> 2
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的嵌套注释控制器测试'destroy'操作.
用户模型has_many :comments, dependent: :destroy
电影模型has_many :comments, dependent: :destroy
评论模型belongs_to :user and :movie
这是我的评论控制器
def create
@comment = @movie.comments.new(comment_params.merge(user: current_user))
if @comment.save
flash[:notice] = 'Comment successfully added'
redirect_to @movie
else
flash.now[:alert] = 'You can only have one comment per movie'
render 'movies/show'
end
end
def destroy
@comment = @movie.comments.find(params[:id])
if @comment.destroy
flash[:notice] = 'Comment successfully deleted'
else
flash[:alert] = 'You are not the author of this comment'
end
redirect_to @movie
end
private
def comment_params
params.require(:comment).permit(:body)
end
def …
Run Code Online (Sandbox Code Playgroud) 我想在我的控制器中添加服务对象。但是销毁操作似乎无法正常工作-基本上它不会删除任何内容,只是重定向到没有Flash消息的页面。
user_stock_destroyer.rb
class UserStocksDestroyer
def initialize(current_user, params, flash)
@current_user = current_user
@params = params[:id]
@flash = flash
end
def call
stock = Stock.find(params)
@user_stock = UserStock.where(user_id: current_user.id, stock_id: stock.id).first
@user_stock.destroy!
flash[:notice] = 'Stock successfully removed'
end
private
attr_reader :current_user, :params, :flash
end
Run Code Online (Sandbox Code Playgroud)
user_stocks_controller.rb
class UserStocksController < ApplicationController
def destroy
UserStocksDestroyer.new(current_user, params, flash)
redirect_to my_portfolio_path
end
end
Run Code Online (Sandbox Code Playgroud) 在我的日志方法中,我想实现某种机制,该机制将忽略两个参数created_at
,而忽略updated_at
(从另一个位置将是例如密码)new_data: form
。form
是管理员参数的哈希值- AdminUser.new(form)
(例如电子邮件,名称,密码等)
方法如下:
def log_admin_data
admin_user = AdminUser.last
AdminPanelLog.update(
admin_email: admin_user.email,
admin_role: admin_user.role,
type: 'Create new Admin User',
new_data: form, #some kind of method like ignore_attribute(created_at, updated_at)
)
end
Run Code Online (Sandbox Code Playgroud)