Fix error message when trying to create an associated record

This error only happens when the foreign key is missing.

Before this fix the following exception was being raised:

    NoMethodError: undefined method `val' for #<Arel::Nodes::BindParam:0x007fc64d19c218>

Now the message is:

    ActiveRecord::UnknownAttributeError: unknown attribute 'foreign_key' for Model.
This commit is contained in:
Rafael Mendonça França 2014-12-30 20:59:27 -03:00
parent a4d5e83560
commit 04852b875e
4 changed files with 23 additions and 1 deletions

@ -1,3 +1,16 @@
* Fix error message when trying to create an associated record and the foreign
key is missing.
Before this fix the following exception was being raised:
NoMethodError: undefined method `val' for #<Arel::Nodes::BindParam:0x007fc64d19c218>
Now the message is:
ActiveRecord::UnknownAttributeError: unknown attribute 'foreign_key' for Model.
*Rafael Mendonça França*
* When a table has a composite primary key, the `primary_key` method for
SQLite3 and PostgreSQL adapters was only returning the first field of the key.
Ensures that it will return nil instead, as Active Record doesn't support

@ -569,7 +569,7 @@ def where_values_hash(relation_table_name = table_name)
[name, binds.fetch(name.to_s) {
case where.right
when Array then where.right.map(&:val)
else
when Arel::Nodes::Casted, Arel::Nodes::Quoted
where.right.val
end
}]

@ -273,6 +273,14 @@ def test_create_association_with_bang_failing
assert_equal account, firm.reload.account
end
def test_create_with_inexistent_foreign_key_failing
firm = Firm.create(name: 'GlobalMegaCorp')
assert_raises(ActiveRecord::UnknownAttributeError) do
firm.create_account_with_inexistent_foreign_key
end
end
def test_build
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save

@ -72,6 +72,7 @@ class Firm < Company
# Oracle tests were failing because of that as the second fixture was selected
has_one :account_using_primary_key, -> { order('id') }, :primary_key => "firm_id", :class_name => "Account"
has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account"
has_one :account_with_inexistent_foreign_key, class_name: 'Account', foreign_key: "inexistent"
has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
has_one :account_limit_500_with_hash_conditions, -> { where :credit_limit => 500 }, :foreign_key => "firm_id", :class_name => "Account"