bibendi / activerecord-postgres_enum

Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
MIT License
361 stars 24 forks source link

Rails 6.1: ArgumentError: wrong number of arguments (given 3, expected 2) #40

Closed sveredyuk closed 3 years ago

sveredyuk commented 3 years ago

Describe the bug Hi, folks. Caught strange bug Ruby 3.0 + Rails 6.1

# Gemfile
gem 'activerecord-postgres_enum', '~> 1.5'

# my migration file
class CreateItemImages < ActiveRecord::Migration[6.1]
  def change
    create_enum :image_type, %w(mini normal large original)

    create_table :item_images do |t|
      t.enum :image_type, enum_name: :image_type
      t.string :sorce_url, null: false
      t.datetime :discarded_at

      t.timestamps
    end

    add_index :item_images, :discarded_at
  end
end

after rails db:migrate I got:

== 20210111230116 CreateItemImages: migrating =================================
-- create_enum(:image_type, ["mini", "normal", "large", "original"])
   -> 0.0043s
-- create_table(:item_images)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (given 3, expected 2)
.../db/migrate/20210111230116_create_item_images.rb:6:in `block in change'
.../db/migrate/20210111230116_create_item_images.rb:5:in `change'
.../bin/rails:5:in `<top (required)>'
.../bin/spring:8:in `block in <top (required)>'
.../bin/spring:5:in `<top (required)>'

I assume this can be rails 6.1 issue, because in pry I got:

[1] pry(#<CreateItemImages>)> t.enum :image_type, enum_name: :image_type
ArgumentError: wrong number of arguments (given 3, expected 2)
.../.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/activerecord-6.1.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:397:in `column'

Can I somehow workaround this?

bibendi commented 3 years ago

Hi, Volodya! I'll try to fix this within the week.

bibendi commented 3 years ago

@sveredyuk I couldn't reproduce the bug :( https://github.com/bibendi/activerecord-postgres_enum/pull/42

sveredyuk commented 3 years ago

@bibendi Very strange. Just used github version and it's reproducing.

Ok, I will try to debug it myself and let you know.

sveredyuk commented 3 years ago

@bibendi Are you sure used Ruby 3.0? I think it's because of options hash vs named attributes in ruby 3.0 issue.

I changed lib/active_record/postgres_enum/extenstions.rb ColumnMethods#enum method

def enum(name, options = {})
   column(name, options.delete(:enum_name), index: options.delete(:index), **options.except(:enum_name))
end

And it works fine. Can I open the PR?

bibendi commented 3 years ago

Oh, shoot, Ruby 3 is very new for me 😄 Sure, feel free to fix the issue!

sveredyuk commented 3 years ago

Please #43

bibendi commented 3 years ago

Thank you for explaination the bug and your PR! But it takes a little bit more effort to make it works. I've fixed it at #42 and released the gem. Please, try v1.6.0!

EPIC448 commented 3 years ago

@sveredyuk and anyone that has answers i get the similar error on my project when I click the "edit" button after creating a dealership and Cars. It is a ruby based app. https://github.com/EPIC448/car_inventory/tree/main/car_inventory It is open project on github thus it is easy to clone and run through, none of my developer guys could find an answer to this.

What I am getting my website page.

ArgumentError in InventoriesController#update

wrong number of arguments (given 1, expected 2)

  def update
    if @inventory.update_attribute(inventory_params)
      redirect_to(@inventory.dealership(notice: 'Inventory was successfully updated.'))
    else
      render action: 'edit'
 end 

car_inventory/controllers/inventories_controller.rb

# POST dealerships/1/inventories
  def create
    @inventory = @dealership.inventories.build(inventory_params)

    if @inventory.save
      redirect_to(@inventory.dealership, notice: 'Inventory was successfully created.')
    else
      render action: 'new'
    end
  end

  # PUT dealerships/1/inventories/1
  def update
    if @inventory.update_attribute(inventory_params)
      redirect_to(@inventory.dealership(notice: 'Inventory was successfully updated.'))
    else
      render action: 'edit'
    end

 private

  # Use callbacks to share common setup or constraints between actions.
  def set_dealership
    @dealership = current_user.dealerships.find(params[:dealership_id])
  end

  def set_inventory
    @inventory = @dealership.inventories.find(params[:id])
  end

  # Only allow a trusted parameter "white list" through.
  def inventory_params
    params.require(:inventory).permit(:carmodel, :price, :status, :dealership_id)
  end

thank you in advance. Please if I missed an important information to aid in this process, let me know. This is my first attempt at writing on github :)