Comments (5)
I did not get what the problem is?
Rails generates both methods ("pretty" and "not pretty") on purpose -
rails/activerecord/lib/active_record/enum.rb
Lines 279 to 294 in 284baa1
from rails.
I did not get what the problem is?
Rails generates both methods ("pretty" and "not pretty") on purpose -
rails/activerecord/lib/active_record/enum.rb
Lines 279 to 294 in 284baa1
@fatkodima Upon reviewing the command output below, it's evident that the method name US West (Oregon)!
contains spaces and brackets, which deviates from standard programming naming conventions. I've noticed this discrepancy, and it seems there might be an issue with adhering to the naming convention for methods.
Additionally, we recently integrated an enum into our application with the following values. However, we discovered that calling the method for the corresponding keys is not possible without utilizing 'send', primarily due to the naming convention.
Could you kindly clarify if Rails intentionally maintains this naming convention? and Why?
Additionally, @fatkodima, it's worth noting that this change was introduced after the merge of #40992. In previous versions before this merge, we only had the pretty method.
[1] pry(#<BugTest>)> john.methods.select { |method| method.to_s.include? "US"}
=> [:"US West (Oregon)!", :US_West_Oregon_?, :US_West_Oregon_!, :"US West (Oregon)?"]
from rails.
As you can see from the removed tests in the PR you opened, this is intentional. It keeps original enum key names to be able to use exact key names (via send
, for example). And a mangled name to be able to little easier use it in the code, like for:
User.enum foo: {"foo bar" => 1}
you can call User.foo_bar
.
For enums with keys like in the description for this issue, it is a pain to use either of the approaches. But this is how it is done and removing this behavior is at least breaking, without saying that it is not needed.
from rails.
As you can see from the removed tests in the PR you opened, this is intentional. It keeps original enum key names to be able to use exact key names (via
send
, for example). And a mangled name to be able to little easier use it in the code, like for:User.enum foo: {"foo bar" => 1}
you can callUser.foo_bar
.For enums with keys like in the description for this issue, it is a pain to use either of the approaches. But this is how it is done and removing this behavior is at least breaking, without saying that it is not needed.
@fatkodima, I suggest that instead of using either approach, we consider restricting the definition of enum keys to follow a convention friendly to programming methods.
And if you look at pretty method that are generated by Rails they are also not as per the naming convention.
For example: US_West_Oregon_?
- Method name starts with the capital letter.
- Method name ends with _ because actual key name ends with ")". These pretty methods are also not name friendly.
from rails.
Like @fatkodima the current behavior is intentional. I'd actually suggest no use enums like this instead, but if you want to, you will get the current behavior of having two methods to the same enum key.
from rails.
Related Issues (20)
- Is there already a built-in method from the Rails API for finding all NESTED belongs_to associations? HOT 1
- Update getting started guide to include ImportMap and Turbo initalization.
- [7.1] MIME parsing raising errors on valid parameters HOT 1
- Rails Query Issue with Null Value Comparison HOT 1
- [Bug][ActionCable/Zeitwerk] Code reloading doesn't close connections
- find_by called with no paramaters HOT 3
- Should select show a prompt when its attribute is false? HOT 8
- collection_check_boxes hidden field ignores :form option
- Action Cable as Stand Alone server leads to Devise::MissingWarden: error HOT 2
- Postgres prepared statements and array queries with certain syntax result in `PG::InvalidTextRepresentation` HOT 5
- Rubocop binstubs issue causes failures on Windows
- `ActiveRecord::ConnectionAdapters::SQLite3Adapter#initialize` does not correctly create missing parent directories HOT 6
- `Object.as_json` is too optimistic and causes errors in 3rd party code. HOT 4
- `ActiveSupport::Inflector` isolation/conflicts: recommended approach HOT 4
- ActiveRecord/Postgresql composite primary keys breaks count query on includes+references HOT 5
- Individually uncommenting `ActiveRecord` related framework defaults, not enabling respective behaviour HOT 3
- Remove trailing slash from void HTML elements HOT 2
- It is not possible to pass raw JSON string to ActiveRecord JSON type attribute HOT 1
- undefined method `path' for class Selenium::WebDriver::DriverFinder (NoMethodError) with selenium-webdriver 4.20.0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.