Skip to content
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

Non-persisted records try to make HTTP calls for associations with nil foreign_key #343

Merged

Conversation

valo
Copy link
Contributor

@valo valo commented May 15, 2015

If you create a new instance of a Her model and it has a belongs_to association which is nil it will try to fetch a record with ID=nil.

A short example:

user = User.new(name: "Karlin", organization_id: nil)
user.organization # this throws an exception as it tries to fetch GET /organizations

The error from the test in the PR:

  1) Her::Model::Associations handling associations without details returns nil if the foreign key is nil
     Failure/Error: @user_without_organization_and_not_persisted.organization.should be_nil
     Faraday::Adapter::Test::Stubs::NotFound:
       no stubbed request for get /organizations
     # ./lib/her/api.rb:94:in `request'
     # ./lib/her/model/http.rb:56:in `request'
     # ./lib/her/model/http.rb:81:in `get_raw'
     # ./lib/her/model/http.rb:70:in `get'
     # ./lib/her/model/associations/belongs_to_association.rb:84:in `fetch'
     # ./lib/her/model/associations/association_proxy.rb:11:in `nil?'
     # ./lib/her/model/associations/association_proxy.rb:40:in `method_missing'
     # ./spec/model/associations_spec.rb:215:in `block (3 levels) in <top (required)>'

I think the correct behavior is to return nil no matter if the record is persisted or not.

@hubert
Copy link
Contributor

hubert commented Aug 5, 2015

@valo could you rebase with what's in master so we can run the full travisci checks. once that's done, we're G2G.

…in a non-persisted record.

Error:

```
  1) Her::Model::Associations handling associations without details returns nil if the foreign key is nil
     Failure/Error: @user_without_organization_and_not_persisted.organization.should be_nil
     Faraday::Adapter::Test::Stubs::NotFound:
       no stubbed request for get /organizations
     # ./lib/her/api.rb:94:in `request'
     # ./lib/her/model/http.rb:56:in `request'
     # ./lib/her/model/http.rb:81:in `get_raw'
     # ./lib/her/model/http.rb:70:in `get'
     # ./lib/her/model/associations/belongs_to_association.rb:84:in `fetch'
     # ./lib/her/model/associations/association_proxy.rb:11:in `nil?'
     # ./lib/her/model/associations/association_proxy.rb:40:in `method_missing'
     # ./spec/model/associations_spec.rb:215:in `block (3 levels) in <top (required)>'
```
It is enough to have a blank foreign key in order to return the default value.
@valo valo force-pushed the fix-nil-belongs-to-relations-for-new-records branch from 120fcab to cd902d9 Compare August 6, 2015 11:24
@valo
Copy link
Contributor Author

valo commented Aug 6, 2015

Done

hubert added a commit that referenced this pull request Aug 6, 2015
…w-records

Non-persisted records try to make HTTP calls for associations with nil foreign_key
@hubert hubert merged commit fcca2d8 into remi:master Aug 6, 2015
@hubert
Copy link
Contributor

hubert commented Aug 6, 2015

thanks for contributing!

ouranos pushed a commit to ouranos/mno-enterprise that referenced this pull request Sep 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants