relations should raise MissingAttributeError if the key is not loaded #5433

Closed
ThomasSevestre opened this Issue Mar 14, 2012 · 8 comments

Comments

Projects
None yet
7 participants

Say we have this class:

class Test < ActiveRecord::Base
  belongs_to :test_owner
end

The following line should raise ActiveModel::MissingAttributeError

Test.select('id').first.test_owner

instead it returns nil

(tested with rails 3.0.11)

Owner

tenderlove commented Mar 15, 2012

I'm OK with a change like this, but it would have to be done in 4.0. @jonleighton @jeremy any opinion?

Contributor

jeremyf commented May 7, 2012

Just re-pinging @jeremy @jonleighton @tenderlove

Owner

jeremy commented May 7, 2012

I'm not sure. This is a grey area. Should a missing foreign key raise?

Contributor

jeremyf commented May 7, 2012

@jeremy I feel as though it should raise; given a person may not know what scope they are in, for me it would be unexpected behavior to have a nil object.

Owner

jeremy commented May 7, 2012

👍

Member

senny commented Oct 28, 2012

just verified the behavior on master and this seems to be implemented. If I'm not mistaken we can close this.

Contributor

frodsan commented Oct 28, 2012

@steveklabnik I think we should reopen this:

[4] pry(main)> Rails.version
=> "4.0.0.beta"
[5] pry(main)> Tag.select('id').first.person_id
ActiveModel::MissingAttributeError: missing attribute: person_id
from /Users/frodsan/Code/rails/activerecord/lib/active_record/attribute_methods/read.rb:96:in `block (2 levels) in read_attribute'
[6] pry(main)> Tag.select('id').first.person
=> nil

@steveklabnik steveklabnik reopened this Oct 28, 2012

Member

senny commented Oct 28, 2012

@steveklabnik sorry for the false information, I miss-interpreted the issue 😟

@frodsan thanks for the validation!

@frodsan frodsan closed this in 10f6f90 Oct 28, 2012

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