我正在开发一个Rails应用程序,用户可以将照片上传到他们的个人资料中.每张照片都有一个标题,应该支持表情符号.尽管将表的编码更改为utf8mb4并修改database.yml,但当我尝试在其标题中保存带有表情符号的照片时,MySQL会返回错误"错误的字符串值".
该应用程序正在使用Ruby 2.3.0开发Rails 5.0.0.1,MySQL正在使用5.7.16版本.
迁移文件:
class CreateUserPhotos < ActiveRecord::Migration[5.0]
def change
create_table :user_photos do |t|
t.string :title
t.string :filename, null: false
t.integer :visibility, null: false, default: 0
t.belongs_to :user, foreign_key: true, index: true
t.timestamps
end
reversible do |dir|
dir.up do
execute "ALTER TABLE `user_photos` CHANGE `title` `title` VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
end
dir.down do
execute "ALTER TABLE `user_photos` CHANGE `title` `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci;"
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
database.yml文件:
production:
adapter: …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据这篇博客文章编写一个 RSpec 控制器测试来检查竞争条件,但创建的Threads 不会“看到”User在测试开始时创建的 s。博客文章建议更改config.use_transactional_fixtures为false,但即使使用此设置Users 也不可见。
这是测试:
it "avoids race conditions" do
u = create(:user, :without_settings)
u_2 = create(:user, :without_settings)
wait_for_it = true
threads = Array.new(4).map {
Thread.new do
true while wait_for_it
# send the request
signed_post(
u,
api_v1_blockades_path,
params: {
blockade: {
blocked_user_id: u_2.id,
reason: 11
}
}
)
end
}
wait_for_it = false
threads.map(&:join)
expect(u.blockades.count).to eq(1)
end
Run Code Online (Sandbox Code Playgroud)
对于身份验证,我使用ApiAuth gem:
before_action :api_authenticate
private
def current_user …Run Code Online (Sandbox Code Playgroud)