romnoks
romnoks

Reputation: 131

Method .save raises ArgumentError

I updated Ruby and Rails version of my project.

Ruby 2.2.3 –> 2.5.1

Rails 4.1.8 –> 5.1.7

When I try to register a new user, I get an error:

ArgumentError (wrong number of arguments (given 2, expected 0..1)):
   app/controllers/users/registrations_controller.rb:41:in `create'

This happens both from client side (frontend) and from Rails console.

registrations_controller.rb:

39: def create
40:   build_resource(sign_up_params)
41:   resource_saved = resource.save
42:
43:   yield resource if block_given?
44:
45:   if resource_saved
46:     if resource.active_for_authentication?
        ...

In Rails console:

user = User.new(email: "[email protected]", name: "qwe qwe", password: '123123123')

=> #<User id: nil, email: "[email protected]", created_at: nil...

user.save!

ROLLBACK
Traceback (most recent call last):
        1: from (irb):4
ArgumentError (wrong number of arguments (given 2, expected 0..1))

User model:

class User < ActiveRecord::Base
  extend FriendlyId
  include InstantSearch
  include PushNotificationToFollowers
  include CarrierWave::MiniMagick

  serialize :site_link, Array
  serialize :followers_ids, Array

  acts_as_token_authenticatable
  acts_as_follower
  acts_as_followable
  acts_as_liker
  acts_as_likeable

  attr_accessor   :site_link_raw

  mount_uploader :avatar, AvatarUploader

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         omniauth_providers: [:facebook, :google_oauth2]

  validates :name, presence: true
  validates :slug, uniqueness: true
  validates_format_of :slug, with: /\A\d+[a-zA-Z]+\S*\z|\A[^\s\d]\S*\z/i, message: 'minimum one letter, without spaces', on: :update

  friendly_id :slug_candidates, use: [:slugged, :finders]

  {some associations}

  before_create :check_slug
  after_create :set_membership
  after_create :send_welcome_email
  after_create :set_settings

  ransacker :id,
            formatter: ->(bool) {
              users_ids = SoundUploader.select(:user_id).where(active:true)

              if bool == 'true'
                users_ids.present? ?  where("users.id IN (#{users_ids.to_sql})").map(&:id) : nil
              else
                where("users.id NOT IN (#{users_ids.to_sql})").map(&:id)
              end

            } do |user|
      user.table[:id]
  end


  def check_slug
    unless self.slug.match(/\A\d+[a-zA-Z]+\S*\z|\A[^\s\d]\S*\z/i)
      new_slug = "project-#{self.slug}"
      user = User.find_by(slug: new_slug)
      new_slug = "#{new_slug}-#{DateTime.current.to_i}" if user
      self.slug = new_slug
    end
  end

  def set_membership
    self.memberships.create
  end

  def send_welcome_email
    UserMailer.welcome_email(self.id).deliver
  end

  def set_settings
    self.create_setting
  end

With other models, I don't get this error.

But with User model, no matter how I call save method, this error always appears.

Upvotes: 2

Views: 291

Answers (1)

romnoks
romnoks

Reputation: 131

In Rails 4.x, an Active Record model inherits from ActiveRecord::Base. In Rails 5.x, all models inherit from ApplicationRecord.

ApplicationRecord is a new superclass for all app models, analogous to app controllers subclassing ApplicationController instead of ActionController::Base. This gives apps a single spot to configure app-wide model behavior.

When upgrading from Rails 4.x to Rails 5.x, you need to create an application_record.rb file in app/models/ and add the following content:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

Then make sure that all your models inherit from it.

I created a new app/models/application_record.rb file with the following contents:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

And changed User model as @mechnicov pointed out.

From:

class User < ActiveRecord::Base
  ...
end

To:

class User < ApplicationRecord
  ...
end

Upvotes: 1

Related Questions