-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
read_attribute(:attr)
and self[:attr]
not equivalent
#155
Comments
I agree, |
This issue is starting to age, but it remains a valid concern still. Now that |
A few thoughts:
I'd recommend:
Happy to make the change to the style guide, just let me know if that's a decision we can agree on! |
Sorry to revisit this, but I recently went into the same problem. I agree we should we remove this from the styleguide. This rule seems kind of random and arbitrary in my point of view. I always thought that read_attribute is actually better and it is extensively used (specially on Rails). |
@bbatsov Do you support a change like this? It sounds like @KevinBongart would be up to draft new language. |
@strand this is actually very Rails specific, not language... I always prefered the usage of Even |
@filabreu Apologies for my lack of clarity. I mean that Kevin has indicated interest in rewriting the language used in this section, not anything to do with the core language or its libraries. |
@KevinBongart There is no doubt a consensus regarding the necessity of the change. A PR would be sufficient if you refer to the ticket there. I'll close the ticket as the discussion has come to an end. |
Question: Why this issue is open again? I'm here because I would like to know more about this. |
Since there seems to be an agreement on simply adding a clarification for this guideline, I created a PR for it. I don't see a reason to recommend |
I'd just point out something that wasn't immediately obvious to me, regarding when ActiveRecord's documentation has this example: person = Person.new(name: 'Francesco', age: '22')
person[:name] # => "Francesco"
person[:age] # => 22
person = Person.select('id').first
person[:name] # => ActiveModel::MissingAttributeError: missing attribute: name It's important to note what happens if a random attribute is used: person[:wrong_attr] # => nil
person.read_attribute(:wrong_attr) # => nil So, the exception is raised when the model does have the attribute, but it's not part of the instantiated object, as when
Again, "missing" has a specific meaning here. |
FWIW, using Notably it's not 100% on par with a hash, since hashes don't raise errors on missing keys. |
My team is using RuboCop with Rails cops enabled as a first step in cleaning up a legacy codebase. We found some code like this:
Among many others, we got this warning from RuboCop:
which refers to this recommendation in the style guide.
And our specs blew up. :( We got several of these:
It turns out that
ActiveRecord::AttributeMethods::Read#read_attribute
andActiveRecord::AttributeMethods#[]
are not exact synonyms: the latter will raise anActiveModel::MissingAttributeError
if the attribute is not found, whereas the former (I think) just returnsnil
and fails silently.It's true that our example is especially gnarly code (
read_attribute
in anafter_initialize
callback), but I thought I should at least leave a note here that the two methods can't always be swapped safely. Would it be helpful to reword the style recommendation to make that more clear?The text was updated successfully, but these errors were encountered: