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

Add section for hash syntax of enums #255

Merged
merged 1 commit into from Jul 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.adoc
Expand Up @@ -385,6 +385,26 @@ class Transaction < ActiveRecord::Base
end
----

=== Enums [[enums]]
pirj marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I investigated whether === Enums or === Enum would be better. The API says Enum, but the guide says Enums, so I think that Enums would be fine.


Prefer using the hash syntax for `enum`. Array makes the database values implicit
& any insertion/removal/rearrangement of values in the middle will most probably
lead to broken code.

[source,ruby]
----
class Transaction < ActiveRecord::Base
# bad - implicit values - ordering matters
enum type: %i[credit debit]

# good - explicit values - ordering does not matter
enum type: {
credit: 0,
debit: 1
}
end
----

=== Macro Style Methods [[macro-style-methods]]

Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the class definition.
Expand All @@ -403,7 +423,7 @@ class User < ActiveRecord::Base

attr_accessible :login, :first_name, :last_name, :email, :password

# Rails 4+ enums after attr macros, prefer the hash syntax
# Rails 4+ enums after attr macros
enum gender: { female: 0, male: 1 }

# followed by association macros
Expand Down