How do I lookup conflicting classes in Rails?

I'm using Rails 3.1 on a project, and bundler has added quite a few gems that I do not really know the contents of that well.

For some reason in development mode (probably because it keeps unloading classes), I intermittently run into an ActiveRecord::AssociationTypeMismatch. That look like this:

Character(#31360520) expected, got Character(#23815500)

I believe that one of the gems has defined a class "Character" and it's conflicting with my ActiveRecord "Character" model. I'm having trouble searching down the source of this problem as it comes up with a bunch of generic results, and I'm not sure what those numbers are called by the Ruby community (class id?).

So a few questions that I'd love help with:

  1. what is that number in the parens after the class name?
  2. How can I look up where that class is from (in which gem) based on the id reported in the exception?
  3. What is the best practice here for commonly named ActiveRecord models? Should I be namespacing my models, or is the blame more with the author of the gem?

Answers


i'm sort of a noob, so don't consider all i say as an established truth - but i think i might help.

question 1

Not sure, but i suspect those numbers are object_ids of the expected Class. In ruby, classes are objects too (singleton objects, though), so they have an object_id as would any other object (you can call object_id on any object to get its unique id). If you want to get an Hash of every classes loaded in your environment along with their id, you can try something like this in the console :

    all_classes = ObjectSpace.each_object.select {|o| o.class == Class}
    with_ids    = Hash[ all_classes.map {|c| [c,c.object_id] } ]
question 2

In the console, all classes are displayed showing their full namespace path, so you should be able with the above script to distinguish your two Character classes if they belong to a different namespace. If the namespace is identical though, the problem may become hairy because one of your class definition will override the other, depending on the load order. I would not be surprised if using ri in your shell would help you to find the culprit gem in this case.

question 3

I would say : avoid vague, too common names for your models ; but in this case, i'd say the gem is to blame, because someone providing a gem should be really careful not to pollute the namespace, especially if it pollutes the ActiveRecord::Base namespace.


Need Your Help

Sending a c++ object with vector over socket

c++ sockets serialization

I have for quite some time now tried to find a good way to serialize or send a state object over tcp socket. My problem is that I am not able to use any 3. party libraries like boost.

Application Helper Spec, test controller-specific output?

ruby-on-rails rspec tdd ruby-on-rails-3.1 helper

I'm a little unsure as to how to write a test for a helper method which has output that is based on the given controller and action that are requested.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.