我的客户模型具有first_name和last_name领域.我可以使用以下代码查询模型:
Customer.where(first_name: "John").where(last_name "Doe")
Run Code Online (Sandbox Code Playgroud)
但是,我想查询这样的模型:
Customer.where(full_name: "John Doe")
Run Code Online (Sandbox Code Playgroud)
但是我没有full_name列.如何在不创建full_name字段的情况下实现此目的?
我正在将Rails 5.1应用程序升级到5.2。我已经更新了包括Rails在内的所有宝石,最后运行了rails app:update它,为Rails应用程序创建了新文件。
具体来说,创建了两个与我有关的文件。首先config/initializers是一个new_framework_defaults_5_2.rb文件,其次是(在同一目录中)a new_framework_defaults.rb。第一个文件仅包含注释,而第二个文件包含一些新的配置选项。这些选项之一是:
ActiveSupport.halt_callback_chains_on_return_false = false
Run Code Online (Sandbox Code Playgroud)
此方法调用将引发undefined_method错误。我需要用这行代码做什么?
这是堆栈跟踪:
Traceback (most recent call last):
73: from bin/rails:4:in `<main>'
72: from /usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
71: from /usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
70: from /usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
69: from /usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
68: from /usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
67: from /usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
66: from /usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
65: from /usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
64: from /usr/local/rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<main>'
63: from /usr/local/rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke'
62: from /usr/local/rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform'
61: from /usr/local/rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
60: from …Run Code Online (Sandbox Code Playgroud) 我已将 Rails 5 应用程序升级到 Rails 6,并将所有 JS 文件从资产管道迁移到 Webpacker。在开发中一切正常。但是,当我将代码推送到生产环境时,尝试加载页面时出现错误。
我正在使用 heroku 进行生产。构建过程成功完成。
这是我的错误:
ActionView::Template::Error (Webpacker can't find application in /app/public/packs/manifest.json.
Possible causes:
2019-10-24T07:23:04.376725+00:00 app[web.2]: 1. You want to set webpacker.yml value of compile to true for your environment
2019-10-24T07:23:04.376728+00:00 app[web.2]: unless you are using the `webpack -w` or the webpack-dev-server.
2019-10-24T07:23:04.376730+00:00 app[web.2]: 2. webpack has not yet re-run to reflect updates.
2019-10-24T07:23:04.376732+00:00 app[web.2]: 3. You have misconfigured Webpacker's config/webpacker.yml file.
2019-10-24T07:23:04.376734+00:00 app[web.2]: 4. Your webpack configuration is not creating a …Run Code Online (Sandbox Code Playgroud) ruby-on-rails webpack ruby-on-rails-5 webpacker ruby-on-rails-6
我使用 MySQL 在 Rails 中开发了一个基本应用程序。我正在转换为 PostgreSQL 数据库。我完成了数据库设置,除了有many to many关系的查询之外,大多数查询都可以工作。
我的模型看起来像这样:
User has_and_belongs_to_many :locations
Location has_and_belongs_to_many :users
Run Code Online (Sandbox Code Playgroud)
但是,当我执行这行代码时,出现以下错误:
location = current_user.locations.find_by(cookies[:current_location])
Run Code Online (Sandbox Code Playgroud)
产生这个错误:
PG::DatatypeMismatch: ERROR: argument of AND must be type boolean, not type integer
LINE 1: ...n_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $...
^
: SELECT "locations".* FROM "locations" INNER JOIN "locations_users" ON "locations"."id" = "locations_users"."location_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $2
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我正在使用 Elasticsearch 7.3 进行产品搜索。产品标题的格式不一样,但我对此无能为力。
有些标题可能如下所示:
Ford Hub Bearing
Run Code Online (Sandbox Code Playgroud)
还有其他人这样的:
Hub bearing for a Chevrolet Z71 - model number 5528923-01
Run Code Online (Sandbox Code Playgroud)
如果有人搜索“雪佛兰轮毂轴承”,“福特轮毂轴承”产品排名第一,雪佛兰零件排名第二。如果我从产品标题中删除所有额外的文本(型号 5528923-01),雪佛兰部件将根据需要排名第一。
不幸的是,我无法修复产品标题,因此当有人搜索时,我需要能够将雪佛兰零件排名第一Chevrolet Hub Bearing。我只是将 的类型设置name为text并将standard分析器应用到我的索引中。这是我的查询代码:
{
query:{
bool: {
must: [
{
multi_match:{
fields:
[
'name'
],
query: "Chevrolet Hub Bearing"
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud) 这可能更像是一个概念性问题。我有一个用户表,我需要为每个用户提供角色。例如,我希望一些用户创建客户,而一些用户只查看客户。我想我可以创建一个这样的关联:
User has_one :role
Role belongs_to :user
Run Code Online (Sandbox Code Playgroud)
使用这种结构,我可以boolean在Role模型上创建字段,例如can_create_customer. 如果我这样做了,那么我可以使用以下代码来检查基于每个用户的权限:
if role.can_create_customer?
Customer.create(name: "Test")
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来完成同样的事情?
是否有一种快捷方式可以查询 Rails 数据库中包含特定文本段的字段的任何记录?我知道我可以用 a 对每个字段进行编码.where("field_name LIKE ?", "my text"),但是我有几个字段并且想知道是否有更短的方法来做到这一点。
提前致谢。
我正在尝试创建一个模块来处理我include在application_controller.rb. 这是我的模块的self.included功能。
def self.included(clazz)
clazz.class_eval do
rescue_from ActiveRecord::RecordNotFound do |e|
respond(:record_not_found, 404, e.message)
end
rescue_from ActiveRecord::RecordInvalid do |e|
respond(:record_invalid, 422, e.record.errors.full_messages)
end
rescue_from ActionController::ParameterMissing do |e|
respond(:bad_request, 400, e.message)
end
rescue_from StandardError do |e|
respond(:standard_error, 500, e.message)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是StandardError捕获所有错误,包括我在其他块中定义的其他错误rescue_from。
我想实现这样的目标:
begin
rescue ActiveRecord::RecordNotFound => e
rescue ActiveRecord::RecordInvalid => e
rescue ActionController::ParameterMissing => e
rescue StandardError => e
end
Run Code Online (Sandbox Code Playgroud)
我在这里先向您的帮助表示感谢。
我觉得这很简单,但我发现似乎没有任何效果。我正在使用带有 Rails 5 的 PostgreSQL 数据库。我需要运行一个查询来查找在名称列中包含文本字符串的所有产品或在关联模型中找到相同字符串的所有产品。这是我的模型结构。
class NormalBrand < ApplicationRecord
has_many :normal_models
end
class NormalModel < ApplicationRecord
belongs_to :normal_brand
has_many :products
end
class Product < ApplicationRecord
belongs_to :normal_model
end
Run Code Online (Sandbox Code Playgroud)
每个模型都有一个名为“名称”的字段。我试图在我的 Products 控制器中创建一个查询,以查找在 3 个模型“名称”列中的任何一个中找到文本字符串的所有产品。像这样的东西...
@products = Product.where("name like lower(?)", "%#{value}%").or.where("normal_model.name like lower(?)", "%#{value}%").or.where("normal_model.normal_brand.name like lower(?)", "%#{value}%")
Run Code Online (Sandbox Code Playgroud)
我知道上面的查询是完全错误的,我应该有某种 joins 语句,但这就是我需要帮助的地方。提前致谢。
我有以下三个类:
class MessageBuilder
def initialize(template)
@template = template
puts @template.instance_of? MessengerTemplate
end
end
class MessengerTemplate
def initialize
@default_template_id = "111111"
end
end
class JobTemplate < MessengerTemplate
def initialize(name)
@name = name
@template_id = "2222"
end
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试检查传递给的参数是否MessageBuilder#initialize是MessengerTemplate. 如果没有,我需要抛出一个错误。
当我打电话时:
message = MessageBuilder.new(JobTemplate.new("Invoice"))
Run Code Online (Sandbox Code Playgroud)
构造函数中的以下行:
puts @template.instance_of? MessengerTemplate
Run Code Online (Sandbox Code Playgroud)
打印FALSE。
有人可以告诉我我在这里做错了什么吗?
我有以下类初始化鼠标事件的canvas元素.监听器在构造函数中初始化,但是当我findxy在事件被触发时调用时,对变量的引用会this.flag导致错误,因为它们是未定义的,这使我相信监听器this在调用时会丢失其引用findxy.我不完全确定如何解决这个问题.提前致谢.
class Signature {
constructor() {
this.signed = false;
this.prevX = 0;
this.currX = 0;
this.prevY = 0;
this.currY = 0;
this.dot_flag = false;
this.flag = false;
this.canvas = document.getElementById('can');
this.ctx = this.canvas.getContext("2d");
this.w = this.canvas.width;
this.h = this.canvas.height;
this.canvas.addEventListener("touchmove", function(e) {
mobilexy('move', e)
}, false);
this.canvas.addEventListener("touchstart", function(e) {
mobilexy('down', e)
}, false);
this.canvas.addEventListener("touchleave", function(e) {
mobilexy('up', e)
}, false);
this.canvas.addEventListener("mousemove", function(e) {
findxy('move', e)
}, false);
this.canvas.addEventListener("mousedown", function(e) {
findxy('down', e) …Run Code Online (Sandbox Code Playgroud)我正在尝试将我的 javascript 文件移动到 Webpack。我对 Webpack 不是很熟悉,所以我不确定我是否正确编码了其中的任何一个。我正在尝试加载 jquery-ui、popper 和 bootstrap 4。但是,在需要Popper. 请注意,我正在为 Ruby on Rails 使用 Wepacker gem。
我在我的 environment.js 文件中包含了以下代码,以便在每个文件中自动包含 jQuery。
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}))
module.exports = environment
Run Code Online (Sandbox Code Playgroud)
这部分有效。所以,从那里我跑了yarn add jquery-ui。
然后在我的 /pack/application.js 文件中我包含了require('jquery-ui').
从我的 js 文件中,以下代码打印到我的控制台:
$(document).ready(function(){
if (jQuery.ui) {
console.log("loaded");
}
});
Run Code Online (Sandbox Code Playgroud)
在此之后,我尝试popper使用yarn add popper.
然后从我的document.ready函数内部调用 popper我得到一个错误:
$(document).ready(function(){
console.log(window.Popper)
});
Run Code Online (Sandbox Code Playgroud)
错误:
Uncaught …Run Code Online (Sandbox Code Playgroud)