Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate field names when using ActiveHash::Enum #322

Open
hatsu38 opened this issue Aug 29, 2024 · 3 comments
Open

Duplicate field names when using ActiveHash::Enum #322

hatsu38 opened this issue Aug 29, 2024 · 3 comments

Comments

@hatsu38
Copy link
Contributor

hatsu38 commented Aug 29, 2024

Environment

Ruby version: 3.3.4
ActiveHash version: 3.3.1

Description of the issue

When using ActiveHash::Enum in combination with ActiveHash::Base, the field_names method returns duplicate field names. This occurs only when enum_accessor is used.

Steps to reproduce

  1. Create an ActiveHash::Base class with some data
  2. Include ActiveHash::Enum
  3. Use enum_accessor
  4. Call .field_names on the class

Example code

class Sport < ActiveHash::Base
  self.data = [
    {
      key: "tennis",
      category: "ball",
    },
    {
      key: "soccer",
      category: "ball",
    },
  ]

  include ActiveHash::Enum
  enum_accessor :key
end

Sport.field_names
# => [:key, :category, :key, :category]

Expected behavior

The field_names method should return unique field names:

Sport.field_names
# => [:key, :category]

Additional information

This issue does not occur when ActiveHash::Enum is included without using enum_accessor:

class Sport < ActiveHash::Base
  self.data = [
    {
      key: "tennis",
      category: "ball",
    },
    {
      key: "soccer",
      category: "ball",
    },
  ]

  include ActiveHash::Enum
end

Sport.field_names
# => [:key, :category]
@kbrock
Copy link
Collaborator

kbrock commented Aug 29, 2024

Has anyone tested what happens with active record's version of enum?

@hatsu38
Copy link
Contributor Author

hatsu38 commented Aug 30, 2024

I tested this with ActiveRecord's version of enum. Since field_names doesn't exist in ActiveRecord, I used column_names instead.

# == Schema Information
#
# Table name: events
#
#  id                                 :uuid             not null, primary key
#  name                               :string           not null
#  status(draft, published, archived) :string           not null
#  created_at                         :datetime         not null
#  updated_at                         :datetime         not null
#
class Event < ApplicationRecord
  enum :status, { draft: "draft", published: "published", archived: "archived" }
end
bin/rails c                                                                               
Loading development environment (Rails 7.1.4)
[1] pry(main)> Event.column_names
=> ["id", "name", "status", "created_at", "updated_at"]

@kbrock
Copy link
Collaborator

kbrock commented Aug 30, 2024

Thanks. I'll put my comments over in the pr.

Update: oops. This is in response to comments in the other PR.

Thanks for documenting.

Yea, so the question mark methods work fine in rails and they find do the constants, so don't have this issue as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants