Giter VIP home page Giter VIP logo

array_enum's Issues

ArgumentError for invalid values to array_enum

In my model, I use ArrayEnum like this:

class MyModel < ApplicationRecord
  extend ArrayEnum

  array_enum assignables: { ASSIGNABLE1: 1, ASSIGNABLE2: 2 }

  validates :assignables, presence: true
end

Now, if I want to create an instance of MyModel using IRB with an invalid value for assignables like this
MyModel.new(assignables: ["test"]), an ArgumentError is thrown: ArgumentError (test is not a valid value for assignables).

I expected to receive an error on the model instance which I am able to derive using .errors. Is there a way I can achieve that? I have already tried to apply an inclusion validation to the assignables field but without success.

Empty Array parameter saved despite of default value is set

Greetings,

I have a field with default value is set as below:

t.integer :favourite_colors, array: true, null: false, default: [0]
# model def:
array_enum favourite_colors: {"red" => 1, "blue" => 2, "green" => 3}

but when sending parameter as favourite_colors: [] empty array this results in record being saved with empty array instead of default one.

PG::AmbiguousColumn error when multiple tables have an array_enum column with the same name

Hey folks!

In my project I have three models lesson.rb, lesson_module.rb and sequence.rb that are all using array_enum like so

array_enum year_levels: {
    year_6: 1,
    year_7: 2,
    year_8: 3,
    year_9: 4,
    year_10: 5,
    year_11: 6,
    year_12: 7,
  }

I implemented a bunch of filters in the lesson.rb file using scopes

scope :filter_by_year_level, -> (year_levels) { with_any_of_year_levels(year_levels) }
scope :filter_by_module, -> (modules) {joins(:lesson_modules).where(lesson_modules: {id: modules})}
scope :filter_by_sequence, -> (sequences) {joins(lesson_modules: [:sequences]).where(lesson_modules: {sequences: { id: sequences}})}

If a request comes in and I need to filter_by_year_level as well as any of the other two scopes, then I get a PG::AmbiguousColumn since there are a grand total of three year_levels columns across the three tables.

Any easy way to get around this issue? I suppose we could switch this with something like

where("#{table_name}.#{attr_name} #{comparison_operator} ARRAY[:db_values]", db_values: db_values)

What do you think?

Do not raise ArgumentError when assigning a blank value

Looking at array_enum.rb#L39, I think assigning a blank value to ArrayEnum will raise an ArgumentError.

class User < ActiveRecord::Base
  extend ArrayEnum

  array_enum favourite_colors: {"red" => 1, "blue" => 2, "green" => 3}
end

user = User.new(favourite_colors: [""]) # raises ArgumentError!

However, Rails (ActiveRecord) implementation of Enum do not raise an ArgumentError when assigning a blank value.

https://github.com/rails/rails/blob/v7.0.2.3/activerecord/lib/active_record/enum.rb#L155-L159

I don't want to raise an ArgumentError if the value is blank (I want to validate it with Subset Validator).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.