1

I am using latest ruby 3.3.3 and latest rails 7.1.3.4.

I am new to rails and I am learning it. Sorry if my request seems dumb to some.

I got this error when I run tests.

It seems that ActiveRecord is unable to prefill them with Time.now. According to the documentation here, it should do so.

I tried to reproduce the issue in a clean install but it worked just fine. I don't understand what's going wrong in my project, especially it's almost the same code.

Here is how my model looks like:

class Drive < ApplicationRecord
  with_options presence: true do
    validates :name
    validates :provider
    validates :credentials
  end

  encrypts :credentials
end

The migration file:

class CreateDrives < ActiveRecord::Migration[7.1]
  def change
    create_table :drives do |t|
      t.string :name
      t.string :provider
      t.text :credentials

      t.timestamps
    end
  end
end

The fixtures:

# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
  name: MyString
  provider: MyString
  credentials: MyText

two:
  name: MyString
  provider: MyString
  credentials: MyText

When looking at test.log I find these entries:

  [1m[36mTRANSACTION (0.0ms)[0m  [1m[36mbegin transaction[0m
  [1m[36mFixtures Load (0.2ms)[0m  [1m[31mDELETE FROM "drives";
INSERT INTO "drives" ("name", "provider", "credentials") VALUES ('MyString', 'MyString', 'MyText');
INSERT INTO "drives" ("name", "provider", "credentials") VALUES ('MyString', 'MyString', 'MyText')[0m
  [1m[36mTRANSACTION (0.0ms)[0m  [1m[31mrollback transaction[0m

I couldn't find what's going wrong and fix it. Help is much appreciated

1 Answer 1

1

I found where the issue is and fixed it!

In fact I had an error when generating my model that said:

Rails cannot recover the underscored form from its camelcase form 'Drive'.
Please use an underscored name instead, either 'drive' or 'drife'.
Or setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.

I didn't give it much importance at first and just used the underscored name instead 'drive'.

It seems it wasn't the right way to do it.

Now, I edited config/initializers/inflections.rb by adding this entry:

inflect.irregular "drive", "drives"

Everything is working fine now!

1
  • 1
    Rails' default behavior is "drives".singularize #=> "drife" due to this rule: inflect.singular(/([lr])ves$/i, '\1f') It's also used to derive a class name (Drife) from a table name (drives).
    – Stefan
    Commented Jun 30 at 10:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.