Ruby

Minitest会抛出ActiveRecord::NotNullViolation

以下错误 ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "user_id" of relation "promotion_covers" violates not-null constraint 在迁移和数据库指示的情况下,显得有些蹊跷: create_table :promotion_covers do |t| [...] t.references :user, null: true,

Ruby中的负Unix时间戳

我有如下的Unix时间戳: time = Time.at(-8_640_000_000_000) # => -271821-04-19 19:00:00 -0500 time.strftime('%B %-d, %Y at %-I:%M %p') # => "April 19, -271821 at 7:00 PM" 但在Discord通过时间戳格式显示同一时间时: # => November 19, 271817 at 7:03 PM 显示的时间戳差异极大。我猜测这

Heroku的 Ruby构建包在第二次部署时崩溃

我把Ruby从 Ruby 3.1.7升级到Ruby 3.2.8;之后,我遇到一种情况:在Heroku部署的服务器上首次部署时Ruby bundle能正常工作,但随后的所有部署在bundle install这一步都失败。这个问题在我尝试的两台服务器上都能重现。用多种方式清除缓存都没解决问题,回滚服务器也不能重新部署,带着新改动且清除了缓存的重新部署也没解决问题。问题在本地环境中不会出现,开发/测试环境之间只是多了capybara、pry、rubocop等测试模块。下面是部署推送时遇到的错误: remote:

在ActiveRecord查询中使用joins时,出现“RuntimeError: unknown class: Integer (RuntimeError)”错误

我有一个ActiveRecord查询,如下所示: Appn.where(stage: :final) .where('event_time < ?', Time.current) .joins(' meetings ON meetings.application_id = applications.id AND meetings.staff_member_id = ?', staff_member.id ) 但当我尝试访问它产生的结果时(例如调用 .to_a),我看到以下错误: RuntimeError:

从零开始搭建的Rails 8.0.4应用在执行测试(bin/rails test)时,line_filtering.rb的参数个数不对

我新建了一个Rails项目,并生成了一个模型,具体如下: rails new nonono --css=bootstrap -j esbuild bin/rails g scaffold State name 之后我运行了测试套件 bin/rails test 并出现了以下错误: $> bin/rails test ... # Running: /home/user/.rvm/gems/ruby-3.4.5/gems/railties-8.0.4/lib/rails/test_unit/line_filt

Rails相当于Laravel的 $loop->first

在Laravel中,当遍历一组事物,比如文章/帖子时,你可以用if语句来检查 if($loop->first),以查看当前项是否是数组中的第一项。然后你可以据此为第一项使用一个不同的UI组件,余下的项再排成网格之类的布局。 Rails的做法是怎样的? 一个更好的示例,说明你可能会这样使用它: @foreach($items as $item) @if($loop->first) Make the first item look different @else render the rest of the

为Timeout::Error和 Errno::ECONNREFUSED编写测试用例

下面的测试: assert_raises(Timeout::Error) do Timeout.timeout(1) do sleep(5) # This should exceed the timeout post confirm_execution_url, params: { txdoc_id: this_txdoc.id }, "headers": { "host": "www.example.com" } end end 似乎并未落入控制器动作的rescue块中 begin @result =

在macOS上执行bundle install时,msgpack gem安装失败

我在MacOS Tahoe(26.3.1(a))。我有一个需要msgpack(1.4.2)的Ruby项目。 很遗憾,当我运行 bundle install 时,看到: Fetching msgpack 1.4.2 Installing msgpack 1.4.2 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. [...] compiling buffer.c buffer.c

在Rails 8中列出所有派生类

我想在Rails中列出某个特定类的所有子孙类(如果需要的话,ApplicationComponent): 启用预加载时,这很容易 ApplicationComponent.descendants 但开发环境通常没有启用预加载,在这种情况下,这将返回 []。由于进行自动加载需要一段时间(在 Zeitwerk::Loader.eager_load_all 的情况下),我希望能用 #eager_load_namespace 只加载我需要的那些: loader = Zeitwerk::Loader.new loa

在脚本中通过require_relative加载环境时,环境的代码会再次执行

真奇怪,我在做一个Rails 8项目,还有一个脚本 bin/odd #!/usr/bin/env ruby puts 'quack' require_relative '../config/environment' puts 'done' 在生产环境中,RAILS_ENV=production bin/odd 报告 quack quack done 在开发环境中,bin/odd 报告了我所期望的结果 quack done 有谁知道这是为什么吗? 解决方案 在调试器里花了几个小时后,我认为这是按预期的行为。