Skip to content

Commit

Permalink
Merge pull request #12918 from versioncontrol/rails-12866
Browse files Browse the repository at this point in the history
Checks to see if the record contains the foreign_key to set the inverse automatically

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Nov 16, 2013
2 parents 17a0b73 + 02ca558 commit c069e0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
* Checks to see if the record contains the foreign key to set the inverse automatically.

*Edo Balvers*

* Exit with non-zero status for failed database rake tasks. * Exit with non-zero status for failed database rake tasks.


*Jay Hayes* *Jay Hayes*
Expand Down
7 changes: 6 additions & 1 deletion activerecord/lib/active_record/associations/association.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ def inverse_reflection_for(record)
# Returns true if inverse association on the given record needs to be set. # Returns true if inverse association on the given record needs to be set.
# This method is redefined by subclasses. # This method is redefined by subclasses.
def invertible_for?(record) def invertible_for?(record)
inverse_reflection_for(record) foreign_key_for?(record) && inverse_reflection_for(record)
end

# Returns true if record contains the foreign_key
def foreign_key_for?(record)
record.attributes.has_key? reflection.foreign_key
end end


# This should be implemented to return the values of the relevant key(s) on the owner, # This should be implemented to return the values of the relevant key(s) on the owner,
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ def test_select_query_method
assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys
end end


def test_select_without_foreign_key
assert_equal companies(:first_firm).accounts.first.credit_limit, companies(:first_firm).accounts.select(:credit_limit).first.credit_limit
end

def test_adding def test_adding
force_signal37_to_load_all_clients_of_firm force_signal37_to_load_all_clients_of_firm
natural = Client.new("name" => "Natural Company") natural = Client.new("name" => "Natural Company")
Expand Down

0 comments on commit c069e0f

Please sign in to comment.