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

Associations now raises `ArgumentError` on name conflicts. #13896

Merged
merged 1 commit into from Jan 31, 2014

Conversation

Projects
None yet
3 participants
@laurocaetano
Copy link
Contributor

laurocaetano commented Jan 31, 2014

It's an attempt to fix #13217.

In order to avoid unexpected behavior, dangerous association names should raise ArgumentError.

This way, models like this:

class Project < ActiveRecord::Base
  has_many :errors
end
# It was deleting all the errors when saving, 
# because the valid? method calls errors.clear. 
# see https://github.com/rails/rails/blob/master/activemodel/lib/active_model/validations.rb#L312-L314

Will raise ArgumentError, saying that the association name will generate an instance method which is already defined by ActiveRecord::Base.

@carlosantoniodasilva

View changes

activerecord/test/cases/associations/has_one_associations_test.rb Outdated
test 'dangerous association name raises ArgumentError' do
[:errors, 'errors', :save, 'save'].each do |name|
assert_raises(ArgumentError, "Association #{name} should not be allowed") do
Post.belongs_to name

This comment has been minimized.

@carlosantoniodasilva

This comment has been minimized.

@laurocaetano

laurocaetano Jan 31, 2014

Author Contributor

Good catch, thanks :)
copy/paste :trollface:

@carlosantoniodasilva

View changes

activerecord/lib/active_record/associations/builder/association.rb Outdated
define_callbacks model, reflection
builder.define_extensions model
reflection
end

This comment has been minimized.

@carlosantoniodasilva

carlosantoniodasilva Jan 31, 2014

Member

I think you don't need to use an else block here, you can left the code untouched and just add the conditional to raise.

This comment has been minimized.

@laurocaetano

laurocaetano Jan 31, 2014

Author Contributor

👍

@carlosantoniodasilva

View changes

activerecord/CHANGELOG.md Outdated
@@ -1,3 +1,19 @@
* Associations now raises `ArgumentError` on name conflicts.

This comment has been minimized.

@carlosantoniodasilva
@carlosantoniodasilva

View changes

activerecord/CHANGELOG.md Outdated
@@ -1,3 +1,19 @@
* Associations now raises `ArgumentError` on name conflicts.

Dangerous association names conflicts includes instance or class method already

This comment has been minimized.

@carlosantoniodasilva

carlosantoniodasilva Jan 31, 2014

Member

I think that'd be include instance or class methods.

@carlosantoniodasilva

View changes

activerecord/lib/active_record/associations/builder/association.rb Outdated
if model.dangerous_attribute_method?(name)
raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but "\
"this will generate an instance method #{name}, which is already defined by Active Record."
end

This comment has been minimized.

@carlosantoniodasilva

carlosantoniodasilva Jan 31, 2014

Member

How about: but this will conflict with a method #{name} already defined by Active Record. Please choose a different association name. so that we direct the user towards a solution.

This comment has been minimized.

@laurocaetano

laurocaetano Jan 31, 2014

Author Contributor

👍

This comment has been minimized.

@chancancode

chancancode Jan 31, 2014

Member

Might want to use an anonymous Class in the tests instead of Post.belongs_to ... because the belongs_to call might have irreversible side-effect on the Post class, which might break other tests cases.

In a real app this wouldn't matter because they except would just blow up the app completely.

@laurocaetano

This comment has been minimized.

Copy link
Contributor Author

laurocaetano commented Jan 31, 2014

@carlosantoniodasilva @chancancode updated with the suggested changes. Thanks guys :)

Associations now raise `ArgumentError` on name conflicts.
Dangerous association names conflicts include instance or class
methods already defined by `ActiveRecord::Base`.

carlosantoniodasilva added a commit that referenced this pull request Jan 31, 2014

Merge pull request #13896 from laurocaetano/dangerous_association_names
Associations now raises `ArgumentError` on name conflicts. Closes #13217.

@carlosantoniodasilva carlosantoniodasilva merged commit 5df5296 into rails:master Jan 31, 2014

1 check passed

default The Travis CI build passed
Details
@carlosantoniodasilva

This comment has been minimized.

Copy link
Member

carlosantoniodasilva commented Jan 31, 2014

Thanks!

@laurocaetano

This comment has been minimized.

Copy link
Contributor Author

laurocaetano commented Jan 31, 2014

Thanks 💚

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