Env :
Ruby : 2.6.0
Rails : 6.1.0
Scenario :
In my project, I have a model Product like below :
# encoding: utf-8
class Product < ApplicationRecord
validates_presence_of :name
...
default_scope -> { order("name ASC")}
end
In console I got error
*** ArgumentError Exception: wrong number of arguments (given 2, expected 1)
when I do simply this :
(byebug) Product.new
*** ArgumentError Exception: wrong number of arguments (given 2, expected 1)
I saw that the behaves of default_scope has been changed in Rails 6.1 by adding **all_queries** PR in Rails But I don’t understand. This should have nothing to do with me.
Any one have same problem ?
Related
Getting below error message when running Rspec after upgrading Ruby to 3.0.3
Failure/Error: FactoryBot.lint strategy: :build_stubbed
FactoryBot::InvalidFactoryError:
The following factories are invalid:
user - wrong number of arguments (given 1, expected 0; required keywords: event_type, object, summary, author) (ArgumentError)
I'm using mongoid-enum gem exactly like the spec
class AdVariation
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Enum
enum medium: [:google, :facebook]
end
and when I run AdVariation.new I get:
ArgumentError: wrong number of arguments (given 1, expected 2..3)
The repo isn't maintained and doesn't answer questions/bugs
Did anyone stumble upon this?
$ bundle show | grep mongoid
* mongoid (5.4.0)
* mongoid-enum (0.4.0)
turns out the documentation on the gem is wrong. The right way is:
enum :medium, [:google, :facebook], default: nil
I had to update from Sendgrid V2 to V3. I am using the Sendgrid-ruby gem 5.3.
I am getting this error
NameError (uninitialized constant PasswordController::Email):
app/controllers/password_controller.rb:54:in `send_email'
May 06 08:57:01 burro-staging app/web.1: ArgumentError (wrong number of arguments (given 1, expected 0)):
Here is the line causing the issue (2nd line below).
mail = SendGrid::Mail.new
mail.from = Email.new(email: 'no-reply#getburro.com') <-----
Ruby is looking for Email class and couldn't find it. The reason is because Email belongs to Sendgrid module, and should be scoped like so:
Sendgrid::Email.new ...
as can be seen here:
https://github.com/sendgrid/sendgrid-ruby/blob/9dd0cf6c9eb7ecc1e4fe2824f9638468ab5fc818/lib/sendgrid/helpers/mail/email.rb
module SendGrid
class Email
attr_accessor :email, :name
def initialize(email: nil, name: nil)
...
And from the docs: https://github.com/sendgrid/sendgrid-ruby#with-mail-helper-class
I am new to RoR. I have two problems here.
ISSUE 1
my rspec is
require 'spec_helper'
describe RefUserCategory do
describe "validations" do
it { should validate_presence_of(:user_category_description) }
end
end
my Ref_User_Category model is
class RefUserCategory < ActiveRecord::Base
validate :user_category_description , presence: true
has_many :Users
end
The error I got in Rspec is
Expected errors to include /can't be blank/ when user_category_description is set to nil, got no errors
So I decided to to use Pry to check whats happening
ISSUE 2
Im my application folder I do
pry -r ./config/environment
[2] pry(main)> Ref_User_Category.new
LoadError: Unable to autoload constant Ref_User_Category, expected /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/736003/east/app/models/ref_user_category.rb to define it
from /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/lib/ruby/gems/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
I have user model too I
pry(main)> User.new
gives error
ActiveRecord::StatementInvalid: Could not find table 'users'
/app-root/data/lib/ruby/gems/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:512:in `table_structure'
Regarding issue 1, you have a typo: change validate to validates.
Regarding issue 2:
You generally start the console from the root of your project like this:
$ rails c
You are doing Ref_User_Category.new when you should do RefUserCategory.new.
About User.new, it looks like you haven't run your database migrations:
$ rake db:migrate
$ rake db:test:prepare
I am using Fabrication gem v-2.5.0 on Rails 2.3.16 but get the following errors when I run the unit test cases:
Below is the code snippet :
First case
Fabricate(:some_modal)
Fabrication::MisplacedFabricateError: # from /Users/user_xyz/.rvm/gems/ree-1.8.7- 2011.03#project/gems/fabrication-2.5.0/lib/fabrication.rb:51:in `Fabricate' from (irb):3
Second case
Fabricate(:some_other_modal)
SyntaxError: /Users/user_xyz/.rvm/gems/ree-1.8.7-2011.03#project/gems/fabrication-2.5.0/lib/fabrication/generator/active_record.rb:8: syntax error, unexpected ':', expecting ')' ...ttributes, without_protection: true)
Can someone please help me resolve these.
Modal Class :
class ErrorCode
attr_accessor :mappings
has_many :error_code_mappings
end
Fabricator :
Fabricator(:error_code) do
application_id 77
error_code_mappings(:count => 3) { |error_code, i| Fabricate.build(:error_code_mapping, :error_code => Fabricate.build(:error_code, :code => error_code.code + i))}
end
Unit test file:
require 'test_helper'
class ErrorCodeTest < ActiveSupport::TestCase
context "ErrorCode" do
setup do
#error_code = Fabricate.build(:error_code)
assert(#error_code.valid?)
end
should "have setter for mapping attribute" do
assert_respond_to(#error_code, :mappings=)
end
end
Fabrication requires Ruby 1.9 and higer version. And the current version of ruby that is being used as per the given code snippets is REE 1.8.7.
Upgrade your ruby version & then u can get it working!