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

Ruby on Rails | Add enums section to Active Record Queries lesson #29099

Open
3 tasks done
sean-garwood opened this issue Nov 20, 2024 · 4 comments · May be fixed by #29116
Open
3 tasks done

Ruby on Rails | Add enums section to Active Record Queries lesson #29099

sean-garwood opened this issue Nov 20, 2024 · 4 comments · May be fixed by #29116
Assignees

Comments

@sean-garwood
Copy link
Contributor

Checks

Describe your suggestion

Introduce enums and show how they can be used.

Path

Ruby / Rails

Lesson Url

https://www.theodinproject.com/lessons/ruby-on-rails-active-record-queries

(Optional) Discord Name

seansg

(Optional) Additional Comments

  • Great intro to metaprogramming
  • Shortcut for queries/scopes
  • Much faster to query ints than strings
@JoshDevHub
Copy link
Contributor

Hi @sean-garwood thank you for making this issue.

I agree that there's probably room for exploring enums in the curriculum. I think if they're going to be mentioned, there should be some content in the lesson itself for them though rather than fully delegating to external sources/links. Would you be comfortable writing some lesson content around enums? I'll offer some guidance on this if you're okay to proceed on that.

Another thing to consider: no project currently requires enums explicitly. If we're going to mention them, we should probably introduce something in one of the projects for the learner to practice with them. One idea I had off the top of my head is that in Private Events, there could maybe be an enum field for the visibility of the events? Let me know if you have any thoughts on this.

@sean-garwood
Copy link
Contributor Author

sean-garwood commented Nov 20, 2024

Would you be comfortable writing some lesson content around enums?

Yep! Happy to add a few paragraphs and some links--maybe after the scopes section?

Another thing to consider: no project currently requires enums explicitly. If we're going to mention them, we should probably introduce something in one of the projects for the learner to practice with them. One idea I had off the top of my head is that in Private Events, there could maybe be an enum field for the visibility of the events? Let me know if you have any thoughts on this.

Agree that it would be worthwhile to practice using them in a project.

I am using them in the final project to track the status of friend requests e.g.

class FollowRequest < ApplicationRecord
  belongs_to :user
  belongs_to :recipient, class_name: "User", foreign_key: "recipient_id"

  enum :status, { pending: 0, accepted: 1, rejected: 2, blocked: 3 }

Seems to fit nicely in any use case where discrete states exist and are used to filter/query records.

@JoshDevHub
Copy link
Contributor

Yep! Happy to add a few paragraphs and some links--maybe after the scopes section?

That makes sense to me. I would probably add both links you have in the OP of this issue (the documentation and the article) to the assignment section of the lesson.

With the actual content, I think after the scopes section seems perfect to me. Some rough guidelines for how I'm envisioning this: (feel free to ask any further questions or push back on something you disagree with)

Do Mention

  • What are enums?
  • Why is this preferred for handling finite, discrete states compared to a plain string column?
  • The syntax for creating them in a Rails application. Talk about the array and hash forms and why the hash form with explicit mappings is preferred.
  • The builtin methods you get for querying and updating a record's enum field (ie. Article.published, @article.published?, @article.published!, etc.).

Avoid Mentioning

  • metaprogramming -- sure this is how enum works but it's also how has_many works, and we don't go into the behind the scenes details on that when teaching associations. Metaprogramming can be a big rabbit role, and I'd rather avoid going into it.
  • I would probably avoid enumerating (hehe) all the different options ie. enum :status { draft: 0, published: 1 }, suffix: true so a query would become Article.draft_status. This stuff is a bit more niche and is better to leave to the docs and the article you have.

Another thing to keep in mind: you'll want to add a point to the Lesson Overview and Knowledge Check sections for enums.

I am using them in the final project to track the status of friend requests e.g.

Yeah, that's a great place to potentially use them. I'd be fine with some callout in that project's specs that mentions enums as a way to manage the state of Friend/Follow Requests.

@JoshDevHub
Copy link
Contributor

Also I'll assign you for this. Again, let me know if you have any questions or concerns!

sean-garwood added a commit to sean-garwood/top_curriculum that referenced this issue Nov 21, 2024
sean-garwood added a commit to sean-garwood/top_curriculum that referenced this issue Nov 22, 2024
chore: model decl

* instance/class methods

Add issue number

fix: big o complexity
sean-garwood added a commit to sean-garwood/top_curriculum that referenced this issue Nov 23, 2024
Add lesson content: section on enums

* use cases
* benefits
  * readability
  * maintainability
  * speed
  * disk space
* how to implement
  * model
  * migration
    * warn about changing persisted data after
      change to enum declaration
* comparison to scopes
* class/instance methods exposed
@sean-garwood sean-garwood linked a pull request Nov 23, 2024 that will close this issue
7 tasks
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

Successfully merging a pull request may close this issue.

2 participants