freeletics / array_enum Goto Github PK
View Code? Open in Web Editor NEWString to integer mapping for PostgreSQL array columns
Home Page: https://github.com/freeletics/array_enum
License: MIT License
String to integer mapping for PostgreSQL array columns
Home Page: https://github.com/freeletics/array_enum
License: MIT License
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.
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.
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?
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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.