Ask A Question

Notifications

You’re not receiving notifications from this thread.

Organizing Rails Model Files Discussion

Great episode, Collin: thank you

I also found the annotate output at the top of the file annoying, so my team now has annotate config set to output at the bottom of the file:

Annotate.set_defaults({
  'position_in_routes'      => "bottom",
  'position_in_class'       => "bottom",
  'position_in_test'        => "bottom"
})
Reply

Thanks, Terry! Glad you liked this one.

Thanks for sharing that annotate config! I've actually never looked into if there was an alternative to placing the comments at the top so this is great to know now.

Reply

+1

Reply

I always wondered about good practices how to order things in my model. Thanks for that and the gotchas.

Reply

Nice one ! I do like to check Mastodon source code as reference too. For example, they've a lot of association on the Account model, and instead of bloating the model, they've defined a Account::Associations module that they include. It allow them to keep associations well organized, with a small comment on related associations. See: https://github.com/mastodon/mastodon/blob/main/app/models/concerns/account/associations.rb

Reply

You can configure Rubocop to enforce an order on your models if you wish using the Layout/ClassStructure rule. https://docs.rubocop.org/rubocop/1.69/cops_layout.html#layoutclassstructure

You can play around with this but something like this:

Layout/ClassStructure:
  Enabled: true
  AutoCorrect: false
  Categories:
    module_inclusion:
      - include
      - prepend
      - extend
    attributes:
      - attribute
      - attr_reader
      - attr_writer
      - attr_accessor
      - alias_attribute
      - delegate
      - enum
    associations:
      - belongs_to
      - has_one
      - has_many
      - has_and_belongs_to_many
    validations:
      - validates
      - validate
      - validates_with
    callbacks:
      - before_validation
      - before_save
      - before_create
      - before_destroy
      - after_initialize
      - after_create
      - after_save
      - after_destroy
      - after_commit
      - after_create_commit
      - after_update_commit
      - after_destroy_commit
      - around_create
    other_macros:
      - acts_as_paranoid
      - audited
      - devise
      - has_paper_trail
      - serialize
    scopes:
      - default_scope
      - scope
    controller_actions:
      - before_action
      - skip_before_action
    controller_rescue:
      - rescue_from
  ExpectedOrder:
    - module_inclusion
    - constants
    - attributes
    - enums
    - associations
    - validations
    - callbacks
    - other_macros
    - scopes
    - controller_macros
    - controller_actions
    - controller_action_caching
    - controller_rescue
    - class_methods
    - initializer
    - public_methods
    - protected_methods
    - private_methods
Reply

That's really awesome to know about, thanks for sharing, Andrew!!!!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.