Skip to content
This repository

Model method names should not conflict with attribute names #74

Merged
merged 1 commit into from about 1 year ago

3 participants

Mike Burns Dan Croak Joe Ferris
Mike Burns
Owner

No description provided.

best-practices/README.md
... ...
@@ -56,6 +56,7 @@ Rails
56 56
   change can be solved with another migration.
57 57
 * Validate the associated `belongs_to` object (`user`), not the database
58 58
   column (`user_id`).
  59
+* Don't name methods after database columns in the same class.
1
Dan Croak Owner
croaky added a note March 15, 2013

The guidelines are alphabetized within each section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Dan Croak
Owner

Might be nice to add a "why" in the commit message.

Mike Burns
Owner

Should we have "why"s listed in the document itself, so people can more easily know what to watch for when they violate a best practice?

Joe Ferris
Owner

I'd be concerned that we can succinctly describe the reasons for some of these guidelines, which would make the guide longer and harder to scan. Having the reasoning in the commit message means that you can use git blame to discover the original reason without having it in the actual guide body. Thoughts?

Mike Burns
Owner

That works. I'll push a commit with a longer explanation later.

Mike Burns
Owner

Squashed and gave an explanation.

best-practices/README.md
... ...
@@ -54,6 +54,7 @@ Rails
54 54
   `update_attribute`, and `toggle`.
55 55
 * Don't change a migration after it has been merged into master if the desired
56 56
   change can be solved with another migration.
  57
+* Don't name methods after database columns in the same class.
1
Joe Ferris Owner
jferris added a note March 22, 2013

Can we rephrase this as an "Avoid" rule? I feel like that appropriately expresses the level of resistance developers should feel here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Joe Ferris
Owner

Good to merge.

Joe Ferris
Owner

Was this ever merged?

Mike Burns Model names distinct from database fields
Naming a method in an ActiveRecord object after the DB column causes
issues with Rails validations such as presence and uniqueness. Even if
the object does not have those validations now, wrapping an AR attribute
limits people in the future arbitrarily.

Instead of wrapping a column, instead name the column more simply. For
example, if you need to expose the receipt string as an `AppleReceipt`,
store the value as `encoded_receipt` or `receipt_string`, then provide a
`receipt` method that handles the wrapping.
eea029c
Mike Burns mike-burns merged commit eea029c into from April 19, 2013
Mike Burns mike-burns closed this April 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 19, 2013
Mike Burns Model names distinct from database fields
Naming a method in an ActiveRecord object after the DB column causes
issues with Rails validations such as presence and uniqueness. Even if
the object does not have those validations now, wrapping an AR attribute
limits people in the future arbitrarily.

Instead of wrapping a column, instead name the column more simply. For
example, if you need to expose the receipt string as an `AppleReceipt`,
store the value as `encoded_receipt` or `receipt_string`, then provide a
`receipt` method that handles the wrapping.
eea029c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 1 addition and 0 deletions. Show diff stats Hide diff stats

  1. 1  best-practices/README.md
1  best-practices/README.md
Source Rendered
@@ -52,6 +52,7 @@ Rails
52 52
 
53 53
 * Avoid bypassing validations with methods like `save(validate: false)`,
54 54
   `update_attribute`, and `toggle`.
  55
+* Avoid naming methods after database columns in the same class.
55 56
 * Don't change a migration after it has been merged into master if the desired
56 57
   change can be solved with another migration.
57 58
 * Don't reference a model class directly from a view.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.