rails/rails — pull request #15581 by sgrif (2014-06-08 23:45:45+00:00)
Title: Have AR::Base#[] act consistently for non-existent behaviors

Currently we have an inconcistency between calling `[]` for columns that
exist but were not included in the query, and accessing columns which do
not exist.

class Author < ActiveRecord::Base
connection.create_table(:authors, force: true) do |t|
t.string :name
end
end

author = Author.select('id').first

# Before:
author[:name] # => Raises ActiveModel::MissingAttributeError
author[:non_existent] # => nil

# After:
author[:name] # => Raises ActiveModel::MissingAttributeError
author[:non_existent] # => Raises ActiveModel::MissingAttributeError

Personally, I think it'd make more sense for `[]` to return nil in all
cases, but the documentation states that "It raises
`ActiveModel::MissingAttributeError` if the identified attribute is
missing." so we cannot do that at this time.