From eac319ebb6aa4514994778b069dc42aa99f001aa Mon Sep 17 00:00:00 2001 From: Tymon Tobolski Date: Tue, 20 Aug 2013 12:24:03 +0200 Subject: [PATCH 1/2] Failing spec --- spec/model/associations_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index 862acca8..4dfa5e23 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -249,6 +249,7 @@ builder.use Faraday::Request::UrlEncoded builder.adapter :test do |stub| stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :organization => { :id => 1, :name => "Bluth Company Inc." }, :organization_id => 1 }.to_json] } + stub.get("/users/4") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :organization => { :id => 1, :name => "Bluth Company Inc." } }.to_json] } stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 1 }.to_json] } stub.get("/users/3") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :company => nil }.to_json] } stub.get("/companies/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company" }.to_json] } @@ -264,6 +265,7 @@ @user_with_included_data = Foo::User.find(1) @user_without_included_data = Foo::User.find(2) @user_with_included_nil_data = Foo::User.find(3) + @user_with_included_data_but_no_fk = Foo::User.find(4) end it "maps an array of included data through belongs_to" do @@ -281,6 +283,10 @@ @user_without_included_data.company.id.should == 1 @user_without_included_data.company.name.should == "Bluth Company" end + + it "does not require foreugn key to have nested object" do + @user_with_included_data_but_no_fk.company.name.should == "Bluth Company Inc." + end end context "building and creating association data" do From d59b19efeec0873eff208dfc7a0757b6c09a729f Mon Sep 17 00:00:00 2001 From: Tymon Tobolski Date: Wed, 23 Oct 2013 13:14:29 +0200 Subject: [PATCH 2/2] Do not require foreign key for belongs_to nested models --- lib/her/model/associations/belongs_to_association.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/her/model/associations/belongs_to_association.rb b/lib/her/model/associations/belongs_to_association.rb index 1671ca5e..7e15c24d 100644 --- a/lib/her/model/associations/belongs_to_association.rb +++ b/lib/her/model/associations/belongs_to_association.rb @@ -72,7 +72,8 @@ def create(attributes = {}) # @private def fetch foreign_key_value = @parent.attributes[@opts[:foreign_key].to_sym] - return @opts[:default].try(:dup) if (@parent.attributes.include?(@name) && @parent.attributes[@name].nil? && @params.empty?) || (@parent.persisted? && foreign_key_value.blank?) + data_key_value = @parent.attributes[@opts[:data_key].to_sym] + return @opts[:default].try(:dup) if (@parent.attributes.include?(@name) && @parent.attributes[@name].nil? && @params.empty?) || (@parent.persisted? && foreign_key_value.blank? && data_key_value.blank?) if @parent.attributes[@name].blank? || @params.any? path_params = @parent.attributes.merge(@params.merge(@klass.primary_key => foreign_key_value))