我已经使用 Ruby 3.0.0 从头开始创建了一个新的 Rails 6.1 应用程序。
我已经运行db:create并生成了一个带有一些字符串列的单个模型,后跟rails db:migrate.
我跑了rails test但是得到了这个需要 rexml 错误:
/Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require': cannot load such file -- rexml/document (LoadError)
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `block in require'
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:299:in `load_dependency'
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `require'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/firefox.rb:22:in `<main>'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `block in require'
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:299:in `load_dependency'
from /Users/froop/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `require'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/webdrivers-4.4.1/lib/webdrivers/geckodriver.rb:72:in `<main>'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
from /Users/froop/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in …Run Code Online (Sandbox Code Playgroud) 我正在尝试将设计用户模型的 id 类型更改为 uuid。
我的迁移如下所示:
class ChangeUserIdTypeToUuid < ActiveRecord::Migration[5.2]
def up
change_column :users, :id, :uuid
end
def down
change_column :users, :id, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行迁移时,我收到一个错误:
== 20180909205634 ChangeUserIdTypeToUuid: migrating ===========================
-- change_column(:users, :id, :uuid)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DatatypeMismatch: ERROR: column "id" cannot be cast automatically to type uuid
HINT: You might need to specify "USING id::uuid".
: ALTER TABLE "users" ALTER COLUMN "id" TYPE uuid
Run Code Online (Sandbox Code Playgroud)
那里有一个提示,但我不知道它在暗示我做什么。不是这个:
change_column :users, :id, id::uuid …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的开发数据库播种。其中一个模型Project有与之相关的图像。
我已经在 中放置了占位符图像./db/seed_files/。我的种子文件如下所示:
# Add projects
1000.times do
project = Project.new(
name: Faker::Marketing.buzzwords.capitalize,
description: Faker::Lorem.sentence(rand(1..30))
)
image_file = File.open("./db/seed_files/placeholder_image.png")
project.images.attach(io: image_file, filename: "placeholder_image.png", content_type: "image/png")
project.save
end
Run Code Online (Sandbox Code Playgroud)
这运行良好。它为每个项目附加一张图像。
但是,我想为每个项目添加多个图像。我想我可以多次附加同一张图片。
我努力了:
# Add projects
1000.times do
project = Project.new(
name: Faker::Marketing.buzzwords.capitalize,
description: Faker::Lorem.sentence(rand(1..30))
)
image_file = File.open("./db/seed_files/placeholder_image.png")
rand(1..3).times do
project.images.attach(io: image_file, filename: "placeholder_image.png", content_type: "image/png")
end
project.save
end
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:ActiveStorage::FileNotFoundError。
/Users/greidods/.rvm/gems/ruby-2.6.1/bundler/gems/rails-b366be3b5b28/activestorage/lib/active_storage/service/disk_service.rb:136:in `rescue in stream'
/Users/greidods/.rvm/gems/ruby-2.6.1/bundler/gems/rails-b366be3b5b28/activestorage/lib/active_storage/service/disk_service.rb:129:in `stream'
/Users/greidods/.rvm/gems/ruby-2.6.1/bundler/gems/rails-b366be3b5b28/activestorage/lib/active_storage/service/disk_service.rb:28:in `block in download'
/Users/greidods/.rvm/gems/ruby-2.6.1/bundler/gems/rails-b366be3b5b28/activesupport/lib/active_support/notifications.rb:180:in `block in instrument'
/Users/greidods/.rvm/gems/ruby-2.6.1/bundler/gems/rails-b366be3b5b28/activesupport/lib/active_support/notifications/instrumenter.rb:23:in `instrument' …Run Code Online (Sandbox Code Playgroud) 我想创建一个写入流并在我的数据进入时写入它.但是,我能够创建该文件,但没有写入任何内容.最终,该过程耗尽内存.
我发现问题是我在循环中调用write().
这是一个简单的例子:
'use strict'
var fs = require('fs');
var wstream = fs.createWriteStream('myOutput.txt');
for (var i = 0; i < 10000000000; i++) {
wstream.write(i+'\n');
}
console.log('End!')
wstream.end();
Run Code Online (Sandbox Code Playgroud)
什么都没有写,甚至没有你好.但为什么?如何在循环内写入文件?
我已经了解了gridlover工具。它提供了 SASS 变量,如:
$scale0: (
fontSize: 1em,
line: 1.5em,
autoLineCount: 1,
autoLineHeight: 1.5em
);
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚所有值对应什么。
我知道我可以使用map-get. fontSize显然是用来设置的font-size,line看起来像line-height。
.some-class {
font-size: map-get($scale0, fontSize);
line-height: map-get($scale0, line);
}
Run Code Online (Sandbox Code Playgroud)
但是什么是autoLineCount和autoLineHeight?这些是 SASS 关键字吗?我想拿他们做什么?
我有:
sprintf("%02X" % 13)
Run Code Online (Sandbox Code Playgroud)
哪个输出:
=>"OD"
Run Code Online (Sandbox Code Playgroud)
我希望我的输出是:
=>"%0D"
Run Code Online (Sandbox Code Playgroud)
我试过了:
sprintf("\%%02X" % 13)
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误warning: too many arguments for format string.同样适用于:
sprintf("%%02X" % 13)
Run Code Online (Sandbox Code Playgroud)
是否有可能%单独添加一个领先的sprintf?
我正在尝试测试登录用户是否可以访问管理页面。
我在灯具中设置了一个用户:
user_one:
email: user_one@test.com
encrypted_password: <%= User.new.send(:password_digest, 'password') %>
Run Code Online (Sandbox Code Playgroud)
在测试中,我登录并导航到管理页面。但是测试失败并重定向。
class AdminTestLoggedIn < ActionDispatch::IntegrationTest
test "Log in" do
post user_session_path, params: {user: {
email: users(:user_one).email,
password: "password"
}}
get admin_path
assert_response :success
end
end
Run Code Online (Sandbox Code Playgroud)
我可以看到重定向指向登录页面:
test "Log in" do
...
assert_redirected_to new_user_session_path
end
Run Code Online (Sandbox Code Playgroud)
所以,我好像还没有登录。我哪里出错了?我的猜测是我没有正确处理密码加密。如果是这样,应该怎么做?
我一直在尝试优化查询。我有一个名为的模型Issue和一个名为Labels它们的模型,它们具有多对多的关系。
为了得到列表issues具有labels与每一个物品的给定阵列中的匹配,我使用的名称:
Issue.select('issues.id, count(labels.id) as matching_label_count')
.joins(:labels)
.where(labels: { name: [*labels] })
.having("matching_label_count = #{labels.size}")
.group('issues.id')
Run Code Online (Sandbox Code Playgroud)
使用 pry,我看到查询Issue::ActiveRecord_Relation按预期返回并且它响应ActiveRecord::Calculations方法。但是,当我调用count结果时,出现语法错误:
pry(main)> Issue.select('issues.id, count(labels.id) as
matching_label_count').includes(:labels).where(labels: { name: labels
}).having("matching_label_count = #{labels.size}").group('issues.id').count
(0.9ms) SELECT COUNT(DISTINCT issues.id, count(labels.id) as
matching_label_count) AS
count_issues_id_count_labels_id_as_matching_label_count, issues.id,
count(labels.id) as matching_label_count, issues.id AS issues_id FROM "issues"
LEFT OUTER JOIN "tags" ON "tags"."issue_id" = "issues"."id" LEFT OUTER JOIN
"labels" ON "labels"."id" = "tags"."label_id" WHERE "labels"."name" IN …Run Code Online (Sandbox Code Playgroud) 在本次演讲中,演讲者创建了一个价值类。
在实现它时,他重写#eql?并说在 Java 开发中,习惯用法是无论何时重写#eql?都必须重写#hash.
class Weight
# ...
def hash
pounds.hash
end
def eql?(other)
self.class == other.class &&
self.pounds == other.pounds
end
alias :== eql?
end
Run Code Online (Sandbox Code Playgroud)
首先,#hash方法是什么?我可以看到它返回一个整数。
> 1.hash
=> -3708808305943022538
> 2.hash
=> 1196896681607723080
> 1.hash
=> -3708808305943022538
Run Code Online (Sandbox Code Playgroud)
使用 pry 我可以看到一个整数响应,#hash但我看不到它从哪里继承该方法。它没有在Numeric或 上定义Object。如果我知道这个方法做了什么,我可能会理解为什么它需要与#eql?.
那么,为什么#hash每次被覆盖时都需要eql?被覆盖?
我通过运行创建了一个文件来保存开发凭据
EDITOR="subl --wait" rails credentials:edit --environment development
Run Code Online (Sandbox Code Playgroud)
例如,我可以Rails.application.credentials.vonage在 dev 中访问。
然而,在测试中做同样的事情,我得到nil:
> Rails.application.credentials.vonage
=> nil
Run Code Online (Sandbox Code Playgroud)
如何在不重复的情况下为开发和测试而不是生产设置共享凭据?