ActiveRecord : Name conflict with "association" #3377

Closed
Dorian opened this Issue Oct 20, 2011 · 9 comments

Projects

None yet

3 participants

@Dorian

I have a name conflict with the function "association", because I have a model "Association" and ActiveRecord now use (since 3.1 I think).

The code in ActiveRecord is : (in /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb)

  def define_writers
    name = self.name

    model.redefine_method("#{name}=") do |value|
      association(name).writer(value)
    end
  end

And my error is, when I do Role.first.association :

SystemStackError: stack level too deep
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:41:in `association'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:41:in `association'
from (irb):4
@jonleighton jonleighton was assigned Oct 21, 2011
@jeremyf

@Dorian Are you still having this issue?

(@jeremyf)

@Dorian

I can't say, I changed my model name to "Asso".

@jeremyf

@Dorian Can you close the issue? It is somewhat analogous to saying "I overwrote the response method in my controller and my controllers stopped rendering"

@Dorian

@jeremy It's quite ambigous, I can use the Association model, but, there is an error when there is a relation (belongs_to for instance) with the Association model.

We can forbid developers to use this model name, or use an other method name in Rails internals.

@jeremyf

@Dorian Is this something you think you could write a patch for? If not, I might be able to help.

So the problem was that you had:
```class Foo
belongs_to :association
end


If so, perhaps we need a guard for the AR::Base.belongs_to
@Dorian

@jeremyf I don't know the Rails code very well. And yes, I think a gard for belongs_to (and has_one ?) should be fine.

@jeremyf

@Dorian Reading the Rails Guide concerning Associations, it appears to be a known issue. As such, without a patch, can you close the ticket.

Did you discover this issue via an automated test?

@Dorian

@jeremyf So I close this issue.

PS : I didn't discover it via automated tests, but via manual testing with irb ;), and looking at Rails source code (quite hard to discover).

@Dorian Dorian closed this Apr 29, 2012
@jeremyf

Hmm. I've made use of Shoulda's macros to test the "should belong_to" methods; I don't know if that would've caught it either.

I believe that you could likely have renamed the "belongs_to :association" to "belongs_to :assoc" and it would've worked.

Good luck.

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